Today we will introduce swift functions, swift functions and c # And js statements, but they are quite different from object-c statements. Let's just get started.
1: function -- constant parameter
Func function name (parameter variable: type, parameter variable: type...) {} Description: 1: func is the function keyword
2: {} function body 3: The parameter variable is of the default constant type and cannot be directly modified in the function body.
That is, func A (value: String) and func A (let value: String) are written in the same way, that is, value is A constant.
Example
/* 1: func function keyword 2: both the two parameter variables value and value1 are constants and cannot be modified. 3: SayHello ("hello", "swift ") method for calling a function */func SayHello (value: String, value1: String) {var str = value + "" + value1 println (str)} SayHello ("hello ", "swift") running result hello swift Note: ---------------------- incorrect syntax -------------- func SayHello (value: String, value1: String) {value = "AA" // incorrect syntax in the function, parameter variables are constant by default and cannot be modified}
2: function -- variable parameters
Func function name (var parameter variable: type ,...) {} description 1: func function keyword 2: var parameter variable: type indicates that the parameter variable is a variable, not a constant, and can be modified arbitrarily.
Example
Func SayHello (var value: String) {value = "AA" // value is modified by var, and value is variable println (value)} SayHello ("hello") run result AA
3: function -- default parameter value
Func function name (parameter value: TYPE = default value) {} description
1: func is the keyword of the function.
2: If the parameter value is not assigned a value, the default value is used.
Example
Func SayHello (value: String = "AA") {println ("value = \ (value)")} SayHello (value: "10") // with the parameter SayHello () // value = 10 value = AA
Iii. functions-variable parameters
Func function name (parameter name type...) {} Description: parameter name type... this parameter can be used with N parameters.
Example
Func SayHello (values: Double ...) {for temp in values {println ("temp = \ (temp)")} SayHello (1.1, 1.2, 1.3) run result temp = 1.1 temp = 1.2 temp = 1.3
4: function-External Parameter Name
Func function name (parameter variable: type, parameter description parameter variable: type) Description: 1: The first parameter in the function does not need description 2: parameter description + Parameter Name: type, constitute a parameter call 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") running result str = hello Swift
5. Function Input and Output Parameters
Func function name (inout function variable type) Description: inout rhetoric variable functions can be directly modified internally, and can be obtained externally.
Example:
Func SayHello (inout value: String) {value = "Hello Swift"} var str = "hello" SayHello (& str) // the address of the String passed at this time, println (str) running result Hello Swift
6: function return value -- tuples
/* Func function name (parameter variable: type, parameter description parameter variable: type)-> type 1: func function keyword 2:-> type, return type */
Example
// ------------------------ The returned tuples func SayHello (value: String, nums Value1: String)-> (String, String) {return (value, Value1)} var (A, B) = SayHello ("hello", nums: "swift") println ("A = \ (A), B = \ (B )")
Running result
A = hello, B = swift
7: function return value --- Function
// Auto-Increment Function func Add (num: Int)-> Int {return num + 1} // auto-Increment Function func zj (num: Int) -> Int {return num-1} // defines the return function type func SayHello (num: Bool)-> (Int)-> Int {// Where (Int) -> the Int return parameter is an integer, And the return value is an integer function return num? Add: zj} var num = 10var fun :( Int)-> Int = SayHello (num> 5); num = fun (num) println ("num = \ (num )") running result num = 11
In the subsequent articles, I wrote the swift language I learned to form a series. Because it is a new language, it is inevitable that there are deficiencies. Please give me your comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.
Summary. Send a mind map to end the article