The functions of swift and function pointers, closure closure, and other related contents are introduced

Source: Internet
Author: User

<span style= "FONT-SIZE:24PX;" >//function//demo1 no parameter type func testconcat () {println ("test function");} Testconcat ()//Call Demo1//demo2 Multi-parameter, a return value/* function begins with Func, represents a function v1 represents a parameter, string parameter type, return value type String*/func testconcats (v1 : string,v2:string)->string{return "\ (v1) and \ (v2)";} The function parameter contains the label func testconcatandmark (V1:string,andvalue v2:string,andnumber v3:string)->string{return "\ (v1), \ (v2),    \ (v3) "}//test function func testfunction () {Let V = testconcats (" Hello "," World ");    println (v);    Let V2 = Testconcatandmark ("David", Andvalue: "Dream", Andnumber: "good"); println (v2)}testfunction ()//function returns multivalued func GetInfo (), (String,string,int) {return ("David", "Dream", 100);} Let (V1,v2,v3) =getinfo ();p rintln ("v1=\ (v1), V2=\ (v2), v3=\ (v3)");//function Nesting function func testfunctioninfunction ()->int{var    y = 10;    Func Add () {y+=5;    } add (); return y;} Let V6 = Testfunctioninfunction ();//*******************************//function pointer * * Five-Star Difficulty * * return value is (Int->int), is a function address first in T is the parameter type, and the second int is the return value */fUNC Getfuncpointer (), (int->int) {func addone (num:int)->int{return num+1; Return addone;//returns a function address}var funpointer = Getfuncpointer ();//funpointer is a function pointer var v7 = funpointer (100);//function called Func        Get () (int->string) {func post (count:int)->string{if count>50{return "success"; }else{return "Failed"}} return post;} Let V8 = Get ();//v8 gets the address of the get () function Let V9 = V8 ();//v9 gets the return value of the Get function//function pointer/address as a parameter, function as argument to function, function transfer </span>
<span style= "FONT-SIZE:24PX;" ></span>
<span style= "FONT-SIZE:24PX;" >//closure is a difficult problem, the function of closure is mainly the communication between functions and functions, a bit like block</span>
<span style= "FONT-SIZE:24PX;" ></span><pre name= "code" class= "OBJC" >//(num:int)->bool closure parameter type func hascolsurematch (arr:Int[], VALUE:INT,CB: (num:int)->bool) {for    item in arr{        if CB (item) {            return true;        }    }    return false;} var ar = [20,9.100,34,89,39];var v1 = Hasclosurematch (arr,40,{    (num:int), Bool in    return num >= 40;}); println ("V1 is \ (v1)");



The functions of swift and function pointers, closure closure, and other related contents are introduced

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.