The functions in Swift are divided into the following categories.
1> non-return 2> without a parameter and return to the 3>, there is no return to the 4>, there is a parameter return to the 5>
Two. Take a look at some of the following examples
1> no reference, no return
Func about ()->void{}
Can also be written as Func about (), () {}
Can also be written as Func about () {}
2> without reference and return
Func ReadM ()->string{return "string"}
3> with no return
Func Eat (food:string) {}
4>, there is a return
Func sum (num1:int, num2:int)->int{return NUM1 + num2}
5> has a lot to return
Func Eat (food1: string, Food2: string)--(string,string) {
var First = " water "
var second = " milk "
if food1. Characters. Count > 0{
First = Food1
}
if food2. Characters. Count > 0{
Second = Food2
}
return(first,second)
}
Three. Use of functions note
1> internal and external parameters, as the name implies, outside the function is the external parameter, the function is inside the inner part. The default is from the second parameter, but also the external parameters, if the first parameter would like to be a foreign parameter, precede the variable name with a label on the line, if you do not want the parameter, precede the argument with a _+ space
2> default parameters, if no specific parameters are passed in, you can use the default parameters
3> variable parameters, Swift function parameters can be changed, the following can be added ... But to the same type
4> by default, the parameter is the value of the pass, if you want to rename the outside of the variable, then to pass the address of the variable, add a keyword inout
The 5> function can be nested, when some constants or variables (such as the map need to get 2 places of the landmark, but in 2 functions can not be the 2 landmarks directly to render the selection route), after the function is released, but also requires 2 functions of the constant or variable value, you can nest function to use , you can get the values of 2 constants or variables in a nested function.
The type of the 6> function, each of which can have its own type, can be a basic data type, or it can be a custom type, such as a type with parameters and return value types. The function can also be used as a method parameter or as a return value, in short, in Swift, The use of the function is very flexible. How to use it requires a lot of experience to accumulate.
Swift (three. function)