swift-3-Functions & Closures

Source: Internet
Author: User

1. Declare a function with Func. The calling function uses his name plus the argument list in parentheses. Use the name and return value type of the delimited parameter

Func greet (name:string, day:string), String {

Return "Hello \ (name), today is \ (day)."

}

Greet ("Bob", "Tuesday")

2. Use tuple (tuple) to return multiple values.

Func getgasprices () (double, double, double) {

Return (3.59, 3.69, 3.79)

}

Getgasprices ()

3. The function can accept a variable number of parameters, collected into an array.

Func sumof (Numbers:int ...), Int {

var sum = 0

For number in numbers {

Sum + = number

}

return sum

}

Sumof ()

Sumof (42, 597, 12)

4. Functions can be nested. Inline functions can access the variables of the function whose definition is located. You can use inline functions to organize your code to avoid too long and too complex.

  1  func returnfifreen ()->int{ Span style= "color: #008080;" > 2  var y = 10   3   Func Add () {  4  y + = 5   5   "  6  
7 add () 8 return y 9 } 10 11 ret Urnfifteen ()

5. The function is the first type. This means that the function can return another function.

Func makeincrementer (),  int{    return1 + number      }         return  = makeincrementer ()//Assign the address assigned to a function to another variable
Increment (7)

6. A function can accept other functions as arguments.

Func hasanymatches (List:int[],condition:int, Bool)bool{ forIteminchlist{ifcondition (item) {return true            }      }        return false}func Lessthanten (number:int)-bool{returnNumber <Ten}var Numbers= [ -, +,7, A]hasanymatches (Number,lessthanten)

function closures

7. The function is actually a special case of closures. You can write a closure without a name, just put it in curly braces. Use in to the return value of a specific parameter and body.

Numbers.map ({       in 3 *   number return  result})

There are several options for writing closures. When a closure type is known, such as a callback, you can ignore its arguments and return values, or both. A closure of a single statement can return a value directly.

Numbers.map ({number in 3 * number})

You can refer to a parameter by a number instead of a name, which is useful for very short closures. A closure passes its last argument to the function as the return value.

Sort ([1, 5, 3, 2]) {$ > $}

swift-3-Functions & 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.