Today, we introduce swift functions, the Swift function and the C#,js are generally written, but they differ greatly from the object-c. Don't say much nonsense, just started.
1: Function-- constant parameter
Func function name (parameter variable: type, parameter variable: Type ... {} Description: 1:func is a function keyword
2:{} function Body 3: Parameter variable is the default constant type, cannot be directly modified in function function body
That is, func a (value:string) is the same as the Func a (let value:string) notation, that is, value is a constant.
Example
/*1:func function Keyword 2: Two parameter variables both value and value1 are constants that cannot be modified 3:sayhello ("Hello", "Swift") method of calling the function */func SayHello (value: string,value1:string) { var str = value + "" + value1 println (str)}sayhello ("Hello", "swift") Run result hello Swift NOTE:----------------------error notation--------------func SayHello (value:string,value1:string) { value = " AA "//error notation in function parameter variable default is constant type cannot be modified}
2: Function--variable parameter
Func function name (var parameter variable: type,... {} description 1:func function keyword 2:var parameter variable: type indicates that the parameter variable is a mutable variable, not a constant and can be arbitrarily modified
Example
Func SayHello (var value:string) { value = "AA"//value is modified by Var, value is variable variable println (value)} SayHello ("Hello") running result AA
3: Function--Parameter default value
Func function name (parameter value: type = default value) {}
1:func is the key word for a function
2: If the parameter value is not assigned, the value of the parameter takes the default value
Example
Func SayHello (value:string = "AA") { println ("value=\ (value)")}sayhello (Value: "10")//With parameter SayHello ()// Without running results VALUE=10VALUE=AA
Three: Function--Variable parameter
Func function name (parameter name type ...) {} Description: parameter name Type ... Indicates that the parameter can touch n parameters.
Example
Func SayHello (Values:D ouble ...) {for temp in values{ println ("temp=\ (temp)") } }sayhello (1.1,1.2,1.3) Run result temp=1.1temp=1.2temp= 1.3
4: Function--External parameter name
Func function name (parameter variable: type, parameter description parameter variable: type) Description: 1: The first one in the function does not need parameter Description 2: Parameter description + parameter name: type, which constitutes a parameter invocation method function name (parameter value, parameter description: Parameter value, parameter description: Parameter value ....) )
Example
Joinstr value1:string is a parameter func SayHello (value:string, Joinstr value1:string) { var str = value + value1 println ("str= \ (str)")} SayHello ("Hello", Joinstr: "Swift") Run results Str=hello Swift
5: function input and output parameters
Description of the Func function name (inout function variable type) :
Example:
Func SayHello (inout value:string) { value = "Hello Swift"}var str = "Hello" SayHello (&STR) //pass at this time String address println (str) Run result Hello Swift
6: Function return value--tuple
/* Func function name (parameter variable: type, parameter description parameter variable: type)-type 1:func function keyword 2: type, return type */
Example
------------------------returns the tuple func SayHello (value:string, Nums value1:string), (string,string) { return ( value,value1)}var (A, b) =sayhello ("Hello", Nums: "Swift") println ("A=\ (A), b=\ (B)")
Run results
A=hello,b=swift
7: Function return value---function
Self-add function func Add (num:int)->int{return num + 1}//decrement function func zj (num:int)->int{ return num-1}//definition return function type Func SayHello (Num:bool)--(int)->int{//Where (int)->int return parameter is shaping, return value is shaped function return num? Add:zj}var num = 10var fun: (Int)->int = SayHello (num > 5), num = Fun (num) println ("num=\ (num)") Run result num=11
later in the article, I went back to learn from the Swift language of knowledge written to form a series. Since it is a new language, it is inevitable that there are deficiencies, and you are welcome to advise me. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply
Finally, a summary. Send the mind map as the end of the article
Introduction to Swift-functions