Recently a little busy, busy looking for work, busy to adapt to the new environment. Now, work two weeks, also adapt to come over, and have time to quiet on the line of our previous study. In this note today, the use of functions is recorded. The following code basically is to figure out the function of the amount of use, but there is a "closure" of the content, after we write a separate piece out.
In the process of parameter passing, because there is no external parameter name, the outer parameter name of the first parameter can be omitted, the second one can not omit let zxtest = teststring (1, zc:2) print (zxtest) // The return value is returned as a tuple, and the arguments are passed in the same way as the note above. let ZX = zxteststring (2, Zc:3) print (ZX) //function with variable number of parameters zxzteststring (1,2,3,4) //parameters of the variable type passed in, The value passed in here is different from the one assigned to it, and the 5 printed out is 3 zxzxzteststring (5).
Here is the form of the incoming formal parameter is the form of the function, the first one we named the external parameter name, so in the pass is worth to be reflected. zxzxzxteststring (Zhanxgu:2, Zxsb:3)
Define a function, parameter name: parameter Type- -type of return value func teststring (Zx:int, Zc:int)->string{ // if (ZX>ZC)// {// return "yes"// } else// {// return "no"// }// a three-mesh operator, and the above logic is the same, according to such shoes will appear you more professional, the code is less, refining. return ZX>ZC? "Yes": "No" }// defines a function whose return value type is the form of a tuple of multiple returned values. Func zxteststring (Zx:int, Zc:int) (string,string) { return ZX > ZC? ("Zhang", "Xu"):("Cao", "XI") //continuous declaration on a line must be delimited; (The writing error here is that there is no space between ZX and the > Behind)}// defines a variable number of function Func Zxzteststring (Zx:int ...) { print (ZX) }//parameters for incoming variable type func zxzxzteststring (var zx:int) { ZX = 3 print (ZX) }/ / function passed in parameter is also a function, Zhanxgu is an external parameter name Func zxzxzxteststring (zhanxgu zx:int, ZXSB: (Int)) { ZXSBD (3) Print ("Xiao Xu Xu")}func ZXSBD (zx:int) { print (String (format: "========%d", ZX) }
Swift2.0 Function Learning Notes