In any language, functions are very important, and today we are going to take a preliminary study of the functions in swift.
1. Swift function Default parameters:
①, can be placed in any position of the function;
②, external name and internal name are consistent;
③, call time can not write;
④, defined in the parameters passed in when the function is defined.
Func joinstring (s1:string, toString s2:string, joiner s3:string = "*"), string{ return s1 + s3 + S2}let str = Joinstring ("Hello", ToString: "World", Joiner: "#") println (str)
As can be seen from the above code, joiner is defined as a default parameter, we can write in the call or do not write, see the following code:
Func joinstring (s1:string, toString s2:string, joiner:string = "*"), string{ return s1 + joiner + S2}let St r = joinstring ("Hello", ToString: "World", Joiner: "#") println (str)
At this point the joiner is both an external parameter and an internal parameter, which is equivalent to adding the # number in front of the joiner.
2. Swift Input/Output function (inout)
Func swap (Var a:int, var b:int) {let t = a a = b b = T}var x = 10var y = 19swap (x, y) println ("\ (x), \ (y)")
As can be seen from the above code output, the value of x and y actually does not change, this is a misunderstanding we know that the swap parameter is actually an argument, it is equivalent to the input of 2 integers, and actually x and Y values have not been changed, if we need to change the value of x and Y, Then we should pass in the address of X and Y.
Func swap (inout a:int, inout b:int) {let t = a a = b b = T}var x = 10var y = 19swap (&x, &y) println (" \ (x), \ (y) ")
As can be seen from the code above, when we need to change the actual value of the variable, we should use the input and output function (inout) when the function is called, and also add the & symbol when calling the function.
Conclusion: When we need to modify the external function inside the function, we need to change the function parameter position to the InOut argument, and call the function to take the address.
Swift First Sight Swift function (i)