Swift Chinese Tutorial (iv) functions and closures

Source: Internet
Author: User
Tags bool closure

function functions

Swift uses the FUNC keyword to declare functions, which are called by the function name plus the argument list in parentheses. Use-> to differentiate between parameter names and the types of return values:

Func greet (name:string, day:string)-> String {return  
    "Hello \ (name).  
Greet ("Bob", "Tuesday")

Practice:

Remove day parameter, add a parameter, such as: Today's Lunch Specials ~

Func greet (name:string, day:string)-> string{return  
      
"Hello \ (name).  The second chapter said to use \ () to  
      
embed  
      
the variable} greet (' Joe ', ' Bodhi Zhai ')

Functions use tuples (tuple) to return multiple values:

1 func getgasprices ()-> (double, double, double) {
2 return (3.59, 3.69, 3.79)
9 ·
4 Getgasprices ()

The function can also receive a variable number of parameters, which are collected in an array:

Func sumof (Numbers:int ...)-> Int {  
    var sum = 0 for number in  
    numbers {  
        sum + = number  
    }  
    retur n Sum  
}  
sumof ()  
sumof (42, 597, 12)

Practice:

Write a function and find out the average of its parameters.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Functions can be nested, nested functions can access variables declared by external functions, and nested functions allow you to organize too long or complex functions:

Func returnfifteen ()-> Int {  
    var y =  
    func Add () {  
        y + + 5  
    }  
    Add () return  
    y  
}  
returnf Ifteen ()

In Swift, a function is a first class type, which means that a function can take another function as its return value:

Func makeincrementer ()-> (int-> int) {  
    func addone (number:int)-> Int {return  
        1 + number  
    }
  return AddOne  
}  
var increment = makeincrementer ()  
increment (7)

The function can also receive other functions as its parameters:

Func hasanymatches (list:int[], condition:int-> bool)-> bool {for  
    item in list {  
        if condition (item) {
  return true
        }  
    } return  
    false
}  
func Lessthanten (number:int)-> Bool {  
    return number < Ten  
}  
var numbers = [7]  
hasanymatches (numbers, Lessthanten)

Closure closure

function is actually a special case of closure, you can write an anonymous closure wrapped with curly braces {}, and use in to differentiate between parameters and the return type of the subject:

1 Numbers.map ({
2 (number:int)-> Int in
3 Let result = 3 * number
4 return result
5})

Practice:

Rewrite this closure on all odd numbers return 0

Closures are written in a variety of concise ways. When the return value type is known, such as a delegate callback, you can omit its argument type, its return value class, or both, and the closure of a single statement can implicitly return the value of this statement directly:

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

You can refer to a parameter by number rather than by name, which is useful for very short closures. A closure passes its last argument through parentheses to a function that takes effect immediately:

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

Author: cnblogs Joe.huang

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.