Variable Functions in basic Swift syntax and swift syntax Variables
Import Foundation // variable declaration use // use let to declare a constant, use var to declare a variable constant can only be assigned to it once let name = "cuiyw"; var age: Int = 20; // The type is optional during declaration. If both declarations are assigned values, the compiler will automatically infer the type var money: Double = 200.0; // There is a simpler way to convert a value into a String: Write the value into brackets, and write a backslash let infto = name + String (age) before the parentheses) + "\ (money)"; println (infto); // function // func function name (parameter variable: type, parameter variable: type ...) {} // Note: // 1: func is the function keyword // 2: {} function body // 3: The parameter variable is the default constant type, you cannot directly modify the format of func A (value: String) and func A (let value: String) in the function body. Is the same, that is, the value is a constant. The parameter cannot be modified. //. no parameter no return value func functest () {println ("functest");} // B. if any parameter has no return value, you can set the default value func functest (name: String, age: Int = 23) {println (name + "\ (age)")} functest ("cyw "); // c when this method is called together with method B, call Method B // func functest (var name: String) -> String // {// name = "a1 Variable Parameter cyw"; // println (name); // return name; //} // functest ("cyw"); // 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, you can modify it at will. // d. This method is lost when compared with method B. You can also add an External Parameter name func functest (var personName: String) {name = "a Variable Parameter cyw"; println (name ); // return name;} functest ("cyw"); // e. there are multiple parameters that return values: func functest ()-> (String, Int) {return ("cuiyw", 23 );} // f The inout Declaration of the input and output functions cannot be declared using var let and the parameter cannot be the func hello (inout sayhello say: String) declared by let {say = "hello ";} var hello = "say hello"; hello (sayhello: & hello); println (hello) // g internal function func func1 () {func func2 () {println ("f Uc2 ") ;}func2 () ;}func1 (); // h returns the function func add (num: Int) of the function type-> Int {return num + 1 ;} func sub (num: Int)-> Int {return num-1;} func func3 (num: Bool)-> (Int)-> Int {return num? Add: sub;} var num = 9; var fun :( Int)-> Int = func3 (num> 10); num = fun (num); println (num ); // The func sumOf (numbers: Int...) function that supports variable parameter lengths ...) -> Int {var sum = 0 for number in numbers {sum + = number} return sum} println (sumOf (42,597) println (sumOf (, 12 ))