Swift - 函数

函数

1 - 函数种类:有返回值带参、有返回值无参、无返回值带参、无返回值无参

复制代码
// 有返回值有参
func greet(person: String, alreadyGreeted: Bool) -> String {

    if alreadyGreeted {
       // ...
    }else{
       // ...
    }

    return "0"
}
print(greet(person: "Tim", alreadyGreeted: true))

// 有返回值无参
func sayHelloWorld() -> String {
    return "hello, world"
}

// 无返回值带参
func greet(person: String) {
    print("Hello, \(person)!")

    // 确切地说该函数是返回一个 Void 型特殊值
    // 它是一个空元组,写成 ()
    return() // 当然,你也可以省略不写
}
复制代码

2 - 多重返回值函数

① 使用元组让多个值作为一个复合值从函数中返回

复制代码
// 元组成员值已被命名,获取时可直接用检索找到的最小值与最大值
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
复制代码

② 可选元组返回类型可选元组类型 (Int, Int)? 整个元组是可选的,这就意味着不只是元组中的每个元素值,而元组包含可选类型 (Int?, Int?) 元组中的元素值是可选的

复制代码
// 不能直接将 nil 做为返回值或者参数
func minMax02(array: [Int]) -> (min: Int, max: Int)?{

    // 当想让 nil 达到这一目的时需要用到可选类型
    if array.isEmpty {
        return nil // 如果返回类型不是可选类型,编译报错   -> (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)
}
复制代码

3 - 隐式返回的函数

复制代码
// 标准
func anotherGreeting(eachPerson: String) -> String {
    return "Hello, " + eachPerson + "!"
}

// 隐式:不建议使用
func greeting(everyPerson: String) -> String {
    "Hello, " + everyPerson + "!"
}
复制代码

4 - 参数标签、参数名称

① 每个函数参数都有一个参数标签以及一个参数名称

复制代码
// 参数 firstParameterName 和 secondParameterName
func someFunction(firstParameterName: Int, secondParameterName: Int) {

}
// 调用
someFunction(firstParameterName: 1, secondParameterName: 2)

// 参数标签
// 在参数名称前指定它的参数标签,中间以空格分隔
// 参数 hometown 的标签是 from
func greet(person: String,  whereFrom hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}

// 参数标签的使用能够让一个函数在调用时更有表达力
// 标签 whereFrom 替代了参数 hometown
print(greet(person: "Bill", whereFrom: "Cupertino"))
// Hello Bill!  Glad you could visit from Cupertino.
复制代码

② 忽略参数标签:如果你不希望为某个参数添加一个标签,可以使用一个下划线 _ 来代替一个明确的参数标签

func someFunction02(_ firstParameterName: Int, secondParameterName: Int) {
}
// 标准调用
someFunction02(1, secondParameterName: 2)

// 以下编译不通过
someFunction02(1,2)
someFunction02(firstParameterName: 1,2)

③ 默认参数值:如果不传第二个参数,parameterWithDefault 会值为 12

func someFunction03(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
    
}
someFunction03(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault = 6
someFunction03(parameterWithoutDefault: 4)                          // parameterWithDefault = 12

5 - 可变参数:可以接受零个或多个值,但是一个函数最多只能拥有一个可变参数。我们可以在变量类型名后面加入 ... 来定义可变参数

复制代码
// 算术平均数
func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}

// 参数可传进多个值
print(arithmeticMean(1, 2, 3, 4, 5))// 3.0
复制代码

6 - 输入输出参数

① 函数参数默认是常量,试图在函数体中更改参数值将会导致编译错误。如果你想要一个函数可以修改参数的值,那么就应该把这个参数定义为输入输出参数:在参数名前加 & 符,表示这个值可以被函数修改

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)

7 - 函数类型

① 你可以定义一个类型为函数的常量或变量,并将适当的函数赋值给它

复制代码
// 函数类型是 (Int, Int) -> Int 型
func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}
// 函类类型变量
var mathFunction: (Int, Int) -> Int = addTwoInts
print("Result: \(mathFunction(2, 3))")  // Result: 5

// 同样完全可以让 Swift 来推断其函数类型
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

③ 函数类型作为返回值 | 函数嵌套

复制代码
 1 // 以下两个函数的类型都是 (Int) -> Int 型
 2 func stepForward(_ input: Int) -> Int {
 3     return input + 1
 4 }
 5 func stepBackward(_ input: Int) -> Int {
 6     return input - 1
 7 }
 8 
 9 // 根据布尔值 backwards 来返回 stepForward(_:) 函数 或 stepBackward(_:) 函数
10 func chooseStepFunction(backward: Bool) -> (Int) -> Int {
11     return backward ? stepBackward : stepForward
12 }
13 var currentValue = 3
14 let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
15 
16 // 趋近于 0
17 while currentValue != 0 {
18     print("\(currentValue)... ")
19     currentValue = moveNearerToZero(currentValue)
20 }
21 print("zero!")
22 
23 // 嵌套:默认情况下嵌套函数是对外界不可见的,但是可以被它们的外围函数调用
24 // 一个外围函数也可以返回它的某一个嵌套函数
25 func chooseStepFunctionAgain(backward: Bool) -> (Int) -> Int {
26     
27     func stepForward(input: Int) -> Int {
28         return input + 1
29     }
30     
31     func stepBackward(input: Int) -> Int {
32         return input - 1
33     }
34     return backward ? stepBackward : stepForward
35 }
36 var currentNumber = -4
37 let moveToZero = chooseStepFunction(backward: currentNumber > 0)
38 while currentNumber != 0 {
39     
40     print("\(currentNumber)... ")
41     currentNumber = moveToZero(currentNumber)
42 }
43 print("zero!")
复制代码

 

posted on   低头捡石頭  阅读(40)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2018-04-28 Swift - 运算符 | 断言 | 先决条件
2017-04-28 iOS基础 - KVO
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示