This section will refer to other applications for functions in Swift, including the following:
Default parameter values and variable parameter values, constant parameters, return function types, and function nesting:
First, default parameter values and variable parameter values
The default parameter value is defined as the initial value of the parameter in the declaration of the function, such as:
Func Sayhelloto (name:string,greeting:string = "Hi", punctuation:string = "! {} In the call can be: Sayhelloto (name: "PlayGround", punctuation: "!!!"). ", Greeting:" Hi ")
Because parameters with default parameter values can be swapped sequentially for functions with default parameters, it is not recommended. The print function is the function that has the delimiter and terminator initial values.
For functions with variable-length parameter types,
Func mean (numbers:double ... ), Double { for in numbers { sum+ = number } The return sum/Double (numbers.count)} Call can be: Mean (2) or mean (2, 3,4,5,6)
However, a function can have only a variable length parameter.
Two, constant parameters
Unlike Java and C + +, Swift cannot change the value of a parameter in a function because the type declaration of the value of the passed parameter is let, and a good function avoids the need to change the value of the function argument.
But if you want to change the value of a parameter, you need to declare it:
Func Tobinary (Var num:int), String { return num/2}
Declare the type as VAR,
But this change is only within the function, but it is not changed outside of the function,
This is because this method is passed in by value, not by reference, but by copy inside the function.
If you need a bash function to change the value of an external parameter, you need to add inout
func swapint (inout a:int, inout b:int) { = (b,a)}
var x:int = 1
var y:int = 2
Swapint (&X,&B)
Third, return function type
This is also very simple, the return type is the function is good:
Func tire1 (weight:int), (int), int { return weight> Ten ? tire1 (weight): Tire2 (weight)}
Iv. Nesting of functions
Func Feebyprice (price:int,weight:int),Int {
Func Choose (weight:int) (int), int { return weight>10? tire1 (weight): Tire2 (weight)}
Return ...
}
Syntax that differs from C + + and Java in the Swift language (v)