Swift Basic Syntax III (functions and closures)

Source: Internet
Author: User

1 //Playground-noun:a Place where people can play2 3 ImportCocoa4 //functions and closures5 6 //function, declare the function using the Func keyword:7Func greet (name:string, day:string)String {8     return"Hello \ (name), today is \ (day)."9 }TenGreet ("Bob", "Tuesday") One  A  - //returning multiple values by tuple (tuple) -Func getgasprices ()(double, double, double) { the     return(3.59, 3.69, 3.79) - } - getgasprices () -  + //supports functions with variable-length parameters -Func sumof (Numbers:int ...)Int { +var sum = 0 A      forNumber in numbers { atSum + = Number -     } -     returnsum - } -  - sumof () inSumof (42, 597, 12) -  to //functions can also be nested +Func Returnfifteen ()Int { -var y = 10 the func Add () { *Y + = 5 $     }Panax Notoginseng Add () -     returny the } +  A Returnfifteen () the  + //as a first-class object, a function can either be a return value or be passed as a parameter -  $Func makeincrementer (), IntInt) { $Func AddOne (Number:int)Int { -         return1 + Number -     } the     returnAddOne - }Wuyivar increment =Makeincrementer () theIncrement (7) -  WuFunc hasanymatches (list:int[], Condition:int-Bool)Bool { -      foritem in list { About         ifcondition (item) { $             return true -         } -     } -     return false; A } +  theFunc Lessthanten (Number:int)Bool { -     returnNumber < 10 $ } thevar numbers = [20, 19, 7, 12] the hasanymatches (Numbers, Lessthanten) the  the //closures-essentially, a function is a special closure, and Swift can use {} to declare anonymous closures -  in Numbers.map ({ the(Number:int)Int in theLet result = 3 * Number About     returnresult the     }) the //when the type of closure is known, the following simplified notation can be used theNumbers.map ({number in 3 *Number }) + //parameters can also be used by the position of the parameter, and the following syntax can be used when the last parameter of the function is a closure: -Sort ([1, 5, 3, 2]) {$ > $}

Swift Basic Syntax III (functions and closures)

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.