Swift教程之函数

函数

Swift中的每个函数都又一个类型,由函数的参数类型和返回类型组成。可以像其它类型一样使用此类型,也可以作为参数传递给其他函数,并在函数中返回函数。函数内部可以定义函数,以便在嵌套函数范围内封装有用的功能。


## 定义并调用函数 以下例子定义一个参数是**String**类型、返回参数是**String**类型的参数:
func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

调用以上函数:

print(greet(person: "Anna"))
// Prints "Hello, Anna!"
print(greet(person: "Brian"))
// Prints "Hello, Brian!"

## 函数参数和返回值

无参数的函数

以下定义了一个没有输入参数的函数:

func sayHelloWorld() -> String {
    return "hello, world"
}
print(sayHelloWorld())
// Prints "hello, world"

即便没有输入参数,函数定义时仍然需要函数后的圆括号,当函数被调用,函数名称后跟上一对空的圆括号。

多个参数的函数

函数可以又多个输入参数,参数写在函数名后的圆括号中,并用逗号分隔:

func greet(person: String, alreadyGreeted: Bool) -> String {
    if alreadyGreeted {
        return greetAgain(person: person)
    } else {
        return greet(person: person)
    }
}
print(greet(person: "Tim", alreadyGreeted: true))
// Prints "Hello again, Tim!"

没有返回值的函数

不需要定义返回类型的函数。

func greet(person: String) {
    print("Hello, \(person)!")
}
greet(person: "Dave")
// Prints "Hello, Dave!"

因为没有返回值,函数定义中可以不写返回箭头(->)。

注意

严格来说,任何参数都有返回值。当函数没有定义返回值时,函数返回特殊类型Void。表示一个空元组,写作()

调用函数的返回值可以忽略:

func printAndCount(string: String) -> Int {
    print(string)
    return string.count
}
func printWithoutCounting(string: String) {
    let _ = printAndCount(string: string)
}
printAndCount(string: "hello, world")
// prints "hello, world" and returns a value of 12
printWithoutCounting(string: "hello, world")
// prints "hello, world" but does not return a value

多个返回值的函数

Swift中使用元组类型作为函数的返回值便实现了函数返回多个值:

func minMax(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

由于函数返回值为元组,可以使用下标语法访问元组的元素:

let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)")
// Prints "min is -6 and max is 109"

可选的元组返回类型

若函数可能返回一个没有值的元组类型,则需要让函数返回可选的元组类型。在返回的元组类型后面加上?

注意

一个可选的元组类型(如(Int, Int)?)与包含可选类型的元组(Int?, Int?)不同。前者是整个元组可选,后者仅仅是元组含有可选类型的元素。

若传入的数组参数为空数组,则在函数中尝试访问数组元素时,会抛出运行时错误。要安全处理一个空数组,需要将函数返回值定义为可选类型:

func minMax(array: [Int]) -> (min: Int, max: Int)? {
    if array.isEmpty { return nil }
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

使用可选绑定来处理调用函数后返回的可选类型:

if let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) {
    print("min is \(bounds.min) and max is \(bounds.max)")
}
// Prints "min is -6 and max is 109"

## 函数参数标签和参数名称 每个函数参数都有参数标签和参数名称。参数标签为调用函数时的参数名,参数名称用于函数体内部使用该参数的名称,默认情况下,参数标签就是参数名称。
func someFunction(firstParameterName: Int, secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(firstParameterName: 1, secondParameterName: 2)

参数名称必须唯一,尽管参数标签可以不唯一,但是唯一的参数标签可提高代码的可读性。

指定参数标签

在参数名称前写入参数标签,用空格隔开:

func someFunction(argumentLabel parameterName: Int) {
    // In the function body, parameterName refers to the argument value
    // for that parameter.
}

greet(person:)函数变体:

func greet(person: String, from hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill!  Glad you could visit from Cupertino."

忽略参数标签

在参数名称前使用下划线忽略参数标签。

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(1, secondParameterName: 2)

默认参数值

可以在参数类型后为该参数分配一个默认值,定义默认值后,函数可忽略该参数。

func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
    // If you omit the second argument when calling this function, then
    // the value of parameterWithDefault is 12 inside the function body.
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12

把没有默认值的参数放在有默认值的参数列表前面,因为没有默认值的参数通常对函数意义更重要。

可变参数

可变参数接受零个或多个指定类型的值。调用具有可变参数的函数时可传入该参数不同数量的输入值,通过在参数类型名后书写 ... 编写可变参数。

可变参数在函数体内作为指定类型的数组使用。

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers

参数

一个函数最多只能有一个可变参数,且要写在参数列表最后。

输入-输出参数

函数参数默认为常量,即该参数在函数体内不可修改。若想将参数定义为变量,并在函数体内修改传入值,将该参数定义为in-out参数。

一个函数只能有一个in-out参数,且不能将常量传入in-out参数中。

注意

in-out参数不能有默认值,且可变参数不能标记成in-out参数。

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

调用带有可变参数的函数时,需在可变参数值前带上&符号。

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

## 函数类型 每个函数都有其特定的函数类型,由参数类型和返回类型组成。
func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    return a * b
}

这两个函数的类型为:(Int, Int) -> Int

对于无参数无返回值的函数:

func printHelloWorld() {
    print("hello, world")
}

其函数类型为:() -> Void

使用函数类型

可以将函数类型像任何其它类型一样使用。例如可以将常量或变量定义为函数类型,并分配适当函数:

var mathFunction: (Int, Int) -> Int = addTwoInts

使用该变量名调用函数:

print("Result: \(mathFunction(2, 3))")
// Prints "Result: 5"

可以将相同类型的不同函数分配给同一变量:

mathFunction = multiplyTwoInts
print("Result: \(mathFunction(2, 3))")
// Prints "Result: 6"

像其它类型一样,Swift可通过赋值的函数该函数类型:

let anotherMathFunction = addTwoInts
// anotherMathFunction is inferred to be of type (Int, Int) -> Int

函数参数作为参数类型

可将函数类型作为另一个函数的参数类型。

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"

函数类型作为返回类型

可将函数类型作为另一个函数的返回类型,在返回函数的返回箭头后写入一个完整的函数类型。

func stepForward(_ input: Int) -> Int {
    return input + 1
}
func stepBackward(_ input: Int) -> Int {
    return input - 1
}
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    return backward ? stepBackward : stepForward
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
print("Counting to zero:")
// Counting to zero:
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// 3...
// 2...
// 1...
// zero!

嵌套函数

嵌套函数是在函数体内定义函数。

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!
posted @ 2017-10-02 21:53  chipsKe  阅读(134)  评论(0编辑  收藏  举报