Welcome to Swift (Apple's official Swift document translation and annotations 33)---235~240 page (fifth-function)

Source: Internet
Author: User

Default Parameter values ( parameter defaults )

When defining a function, you can define a default value for any parameter, and if you define a default value, you can ignore this parameter when calling the function.

Attention:

When setting the default value of a parameter, you need to follow the last start of the function's argument list, which ensures that when the function is called, even the parameter without the default value can be in order, making the function clearer when it is called.

The following code example is a new version of the Join function that uses the default parameters:

Func Join (String s1:string, ToString s2:string,

Withjoiner joiner:string = ""), String {

return s1 + joiner + s2

}

If you set a string's default value when calling the Joiner function, the value can be stitched together with another string:

Join (string: "Hello", toString: "World", Withjoiner: "-")

Returns "Hello-world"

However, if the default value is not set when calling joiner, the system will use a space instead:

Join (string: "Hello", toString: "World")

Returns "Hello World"

External Names for Parameters with default values (extensions for defaults )

In how many cases, it is necessary to provide the extension when using the default value for the parameter. This will clarify the meaning of the parameter, especially when calling the function.

For convenience, Swift provides default parameters that are automatically extended to the definition when you do not provide an extension. This auto extension is the same as the local parameter name, as if you had written a # number in front of the local variable.

The following is a version of the Join function that does not provide an extension for the parameter, but provides a default value:

Func Join (s1:string, s2:string, joiner:string = ""), String {

return s1 + joiner + s2

}

In this case, Swift automatically provides an extended parameter name to the parameter that has the default value. Therefore, when calling a function, it is necessary to provide arguments for the extension, which makes the parameters of the function clearer and unambiguous:

Join ("Hello", "World", Joiner: "-")

Returns "Hello-world"

Variadic Parameters ( variable parameter )

A mutable parameter can receive 0 or more values of a certain type. When calling a function, you can use a mutable parameter to specify the number of arguments passed to the function. The variable parameter is written by inserting a three-point character (...) before the parameter type name.

Values passed to a mutable parameter can be used in the body of a function as an array-like type. For example:

Func Arithmeticmean (numbers:double ...), Double {

var total:double = 0

For number in numbers {

Total + = number

}

return total/double (numbers.count)

}

Arithmeticmean (1, 2, 3, 4, 5)

Returns 3.0, which is the arithmetic mean of these five numbers

Arithmeticmean (3, 8, 19)

Returns 10.0, which is the arithmetic mean of these three numbers

Attention:

A function can have at most one variable parameter, and must be at the end of the argument list, in order to avoid ambiguity when calling functions of multiple arguments.

If you define a function with one or more parameters that have a default value, you can also use a mutable parameter, just to place it on the last side of all default parameters.

Constant and Variable Parameters ( parameter constants and parameter variables )

function parameters are constants by default, and attempting to modify the parameter values of a function inside a function results in a compilation error. This means that you cannot modify the parameters of the function.

However, sometimes a variable copy of a parameter value is very useful for a function, so you can avoid defining a new one in the body of the function. A parameter variable can be handled as if it were a variable instead of a constant, and a new copy of the parameter value is copied to it.

Defining a parameter variable is the keyword prefixed with the parameter name: var

Func AlignRight (Var string:string, Count:int, Pad:character), string {

Let Amounttopad = count-countelements (String)

For _ in 1...amountToPad {

string = Pad + String

}

return String

}

Let originalstring = "Hello"

Let paddedstring = AlignRight (originalstring, 10, "-")

PaddedString is equal to "-----Hello"

Originalstring is still equal to "hello"

This example defines a new function called AlignRight, which aligns the input string to the right, and in this case the string ' hello ' is converted to '-----hello '.

The function alignright defines the parameter variable string, which means that the string is a mutable local variable that can be initialized with the passed string value or modified in the body of the function.

This function first calculates how many characters need to be added to the left side of the string to achieve a right-aligned effect. This value is stored in the local constant Amounttopad, and then the function adds Amounttopad number of dash characters to the left of the string, and finally returns the result.

Attention:

Modifiable parameter variables cannot be scoped beyond the end of a function and are not visible to functions. The lifetime of the parameter variable exists only in the call cycle of the function.

To be Continued ... *******//

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.