1 Import Foundation2 //variable declarations use3 4 //use let to declare a constant, use Var to declare a variable constant to assign it only once5Let Name="Cuiyw";6 varAge:int= -;7 //The compiler automatically infers the type when declaring the type is optional, and when the declaration is assigned a value8 varmoney:double=200.0;9 //there is a simpler way to convert a value to a string: write the value in parentheses, and write a backslash before the parenthesesTenLet infto=name+string (age) +"\ (Money)"; One println (INFTO); A - //function - //func function name (parameter variable: type, parameter variable: Type ... ) {} the //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. Parameter cannot be modified - + A //A. No parameter no return value at func functest () - { -println"functest"); - } - - //B. Parameter no return value can set default value inFunc Functest (name:string,age:int= at) - { toprintln (name+"\ (age)") + } -Functest ("Cyw"); the * //C Call the B method when the method is called with the B method $ //func functest (var name:string)->stringPanax Notoginseng //{ - //name= "A1 variable parameter Cyw"; the //println (name); + //return name; A //} the //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 mutable variable, not a constant and can be arbitrarily modified the - //d The method compares with the B method when the respective outputWuyi //method can also add an external parameter name theFunc Functest (varpersonname name:string) - { WuName="A variable parameter Cyw"; - println (name); About //return name; $ } -Functest ("Cyw"); - - A + //E. There are multiple return values for parameters theFunc functest ()(String,int) - { $ return("Cuiyw", at); the the } the //f input/Output function InOut declaration cannot be declared with a var let argument cannot be a the func Hello (inout sayhello say:string) - { insay="Hello"; the } the varhello="Say hello"; AboutHello (sayhello: &hello); the println (Hello) the the + //g intrinsic function - func func1 () the {Bayi func func2 () the { theprintln"FUC2"); - } - Func2 (); the the } the func1 (); the - //h functions that return function types theFunc Add (Num:int),Int the { the returnnum+1;94 } the theFunc Sub (num:int)Int the {98 returnnum-1; About } - 101Func func3 (Num:bool), (INT)Int102 {103 returnNum?add:sub;104 } the varnum=9;106 varFun: (Int)->int=func3 (num>Ten);107num=Fun (num);108println (num);
The variable function of Swift basic syntax