Swift

Swift 3.0之六、函数

2016-12-09  本文已影响21人  麟young

函数是一个独立的代码块,用来执行特定的任务。

1. 定义和调用函数

Swift 函数一般形式为:

func 函数名:(形式参数1: 类型, 形式参数2: 类型, ...) -> 返回类型 {
    // 此函数要做的事儿
    return 返回类型
}

举个栗子:

func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

函数调用形式为:
函数名(实际参数1,实际参数2,...),实际参数要和形式参数的顺序和类型一致。
举个🌰 :

func greetAgain(person: String) -> String {
    return "Hello again, " + person + "!"
}
print(greetAgain(person: "Anna"))
// 结果为: "Hello again, Anna!"

2. 函数的形式参数和返回值

无形式参数的函数

func sayHelloWorld() -> String {  // 函数名后的括号不可省略
    return "hello, world"
}
print(sayHelloWorld())
// 结果为: "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))
// 结果为: "Hello again, Tim!"

无返回值的函数

// 严格意义上,无返回值的函数返回的是 Void 类型,即: "-> Void"
func greet(person: String) {  // "->" 和 "返回类型" 省略
    print("Hello, \(person)!")
}
greet(person: "Dave")
// 结果为: "Hello, Dave!"

有返回值的函数可以不使用其返回值,如下面的🌰 :

func printAndCount(string: String) -> Int {
    print(string)
    return string.characters.count
}
func printWithoutCounting(string: String) {
    let _ = printAndCount(string: string)
}
printAndCount(string: "hello, world")
// 结果为: "hello, world" 并且返回12
printWithoutCounting(string: "hello, world")
// 结果为: "hello, world" 但不返回值

返回值为元组的函数

// 使用元组可以返回多个值,这个例子返回数组的最大和最小值。
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)")
// 结果为: "min is -6 and max is 109"

返回值为可选元组的函数

// 在返回的元组括号后加上 "?" 就变为可选元组
func minMax(array: [Int]) -> (min: Int, max: Int)? {
    if array.isEmpty { return nil }  // 如果数组为空,返回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)
}

3.函数实际参数标签和形式参数名

指定实际参数标签

通用格式为:

func 函数名(实际参数标签名 形式参数名: 参数类型) -> 返回类型{
    // 函数内部使用形式参数名
    // 调用函数时使用实际参数标签名
}

举个🌰 :

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

省略实际参数标签

如果不想使用实际参数标签的话,可以利用下划线(_)代替,如:

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    // 
}
someFunction(1, secondParameterName: 2)

默认形式参数值

形式参数参数类型后赋一个默认值。
如果定义了默认值,调用函数时可以省略这个形式参数,举个🌰 :

func someFunction(parameterWithDefault: Int = 12) {
    print(parameterWithDefault)
}
someFunction(parameterWithDefault: 6) // parameterWithDefault 为 6
someFunction()  // parameterWithDefault 此时为默认值 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)
// 返回5个数字的平均数 3.0
arithmeticMean(3, 8.25, 18.75)
// 返回3个数字的平均数 10.0

注意: 一个函数最多只能有一个可变形式参数

输入输出形式参数

输入输出形式参数不能有默认值,可变形式参数不能标记为inout

举个交换两个变量值的🌰 :

// 定义函数
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)")
// 结果为: "someInt is now 107, and anotherInt is now 3"

4. 函数类型

每一个函数都有一个特定的函数类型,它由形式参数类型 + 返回类型组成,如:

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    return a * b
}

上面两个函数都是(Int, Int) -> Int,读作”形参为两个Int类型,并返回一个Int类型值的类型“。
下面这个函数的类型则为() -> Void,或者“一个没有形式参数的函数,返回Void”:

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

使用函数类型

可以像使用Swift中的其他类型一样使用函数类型,给一个常量或变量定义一个函数类型,并为其指定一个相应的函数,如:

var mathFunction: (Int, Int) -> Int = addTwoInts
// 读作: ”定义一个叫做mathFunction的变量,
// 它的类型是‘一个能接受两 Int 值的函数,并返回一个 Int 值’,
// 将这个变量指向 addTwoInts 函数“。

现在,可以用mathFunction这个变量调用函数,如:

print("Result: \(mathFunction(2, 3))")
// 结果为: "Result: 5"

当然,也可以将变量指向别的同类型的函数,如:

mathFunction = multiplyTwoInts
print("Result: \(mathFunction(2, 3))")
// 结果为: "Result: 6"

未给出常量或者变量类型时,编译器会对类型进行推断:

let anotherMathFunction = addTwoInts
// anotherMathFunction 被推断为 (Int, Int) -> Int 类型

函数类型作为形式参数类型

直接上🌰 :

// 第一个参数为“形参为两个整型返回值为一个整型值”的函数类型
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// 结果为: "Result: 8"

函数类型作为返回类型

举个🌰 :

// 先定义两个(Int) -> Int 类型的函数,
// 一个作用是返回比传入数值大1的数字 另一个作用是返回比传入数值小1的数字 
func stepForward(_ input: Int) -> Int {
    return input + 1
}
func stepBackward(_ input: Int) -> Int {
    return input - 1
}

// 此函数的类型为 Bool -> (Int) -> Int
// 其参数为一个Bool值,返回值是(Int) -> Int函数类型
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
    return backwards ? stepBackward : stepForward
}

// 使用chooseStepFunction函数,返回需要的函数
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero 现在指向 stepBackward() 函数

// 接下来,使 currentValue 变量一步步接近 0
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// 3...
// 2...
// 1...
// zero!

5. 内嵌函数

重写上边的🌰 中的chooseStepFunction(backward:)来使用和返回内嵌函数:

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 现在指向 stepForward() 函数
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!
上一篇 下一篇

猜你喜欢

热点阅读