function is to implement some specific functions of the module plate, call through the function name (the best function name from the function name can be basically know what the function is doing, do not bother, can write more points), you can define one or more parameter names, parameter types, and then you can have a return type, when called by the function name (" Call ") function, we pass the parameter type consistent with the parameter type defined by the function
(i) Defining and invoking functions
We define a function of the SayHello (_:), because that is the function, passing in a name, and returning to greet this person. To accomplish this, we need a string type of incoming parameter called PersonName, a return value to greet the person, and the code is as follows
Override Func Viewdidload () { super.viewdidload ()// func Geet (preson:[string:string]) {// guard Let name = preson["name"] else{// print ("Hello")// return// }// print ("hello\ (name)!") }// // Geet (["Name": "Xiaoyu"])// Geet (["DFDFDF": "A"]) SayHello ("Xiaoyu") } func SayHello (personname:string), string{let greeting = "Hello," + PersonName + "!" Print (greeting) return greeting }
(The code of the note is written when I look at the control flow, and the guard and else want to appear in pairs, rather than as if, else can not appear, personally think that swift out of this is relative if, in accordance with the requirements, this can give us more about the non-conforming operation ( violated requirement), in else we can write error message, can also Break,continue,throw,fataerror () and so on, this is not very practical, if you understand better, you can attach a code below, All right, let's get to the bottom, explain the function above
(1) Func is the function keyword, this will not have to say more
(2) SayHello is the name of the function, which is the identifier of the function, and you can find it when you call it.
(3) Presonname is the name of the parameter, string is the type of the parameter, the middle with: number is divided (c is the first test type, followed by the name of the parameter, such as int A, multiple parameters are also used, number segmentation)
(4)-The return value of the function, string representing the type of the return value
This function is already written, and when I want to say hello to somebody, I can call it, by the name of the function, as in the code above.
The code above can also simplify
Print (SayHello ("Xiaoyu") } func SayHello (personname:string), string{//let greeting = "Hello," + PersonName + "!" print (greeting) return ("Hello,\ (personname)!") }
Come here today, next time I will write the function parameters and return values, please look forward to, thank you
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift Learning function (i)