main.swift// functiontest-04//import foundationprintln ("function test!") TestFunction ()
The functiontest.swift//functiontest-04//import foundation//function begins with a Func representation of a function//parameter and parameter, and//V1 represents the parameter: String representing the type of the parameter//-& Gt Represents the return value Stringfunc testconcat (v1:string, v2:string)->string{return "\ (v1) and \ (v2)";} The parameter is 0, the return value is three func getInfo (), (string,string,string) {return ("Welcome", "Come", "Baidu");//is a meta-ancestor}// The label is the advantage of OC Swift has brought the label inside OC to the Func testConcat2 (v1:string, Andvalue v2:string,andnumber v3:int)->string{return "\ (v1) A nd \ (v2) and \ (v3);} Func testfunctioninfunction ()->int{var y = 10; Add is a function of the LUA scripting language func Add () {//var y = 1; The following y will choose Zui y + = 5; } add (); return y;} This is a function return value is (Int->int) func getfuncpointer (), (int->int) {func addone (num:int)->int {return nu m+1; }//Returns the address of a function return addone;} Func testfunction () {println ("Start test function"); Let V = testconcat ("Hello", "World"); println ("V is \ (v)"); Andvalue: called a label Let V2 = TestConcat2 ("Baidu", Andvalue: "Baidu recruitment", andnumber:100); println ("V2 is\ (v2) "); Let (v3,v4,v5) = GetInfo (); println ("v3\ (v3) v4\ (v4) v5\ (V5)"); Let V6 = Testfunctioninfunction (); println ("V6 Is\ (V6)"); function pointer var funcpointer = Getfuncpointer (); var V7 = funcpointer (199); println ("V7 is \ (V7)"); Take the function address/pointer as the parameter var arr = [20,10,333,4,43,33]; var V8 = Hasanymatch (Arr,lessthanten);} Func Hasanymatch (list: [Int],condition:int, Bool), bool{for item in list{if (condition (item)) {return true; }} return false; }func Lessthanten (number:int)->bool{return number <= 10;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift-functions, function pointers