#6 函数

swift的函数和JS的函数基本类似,有几点不同的是:

  • swift中的函数参数默认是带参数标签的,例如:someFunc(firstParam: "james", age: 18)
  • 可变参数的表达方式,使用 ... 和js类似,但是其书写方式不同:arithmeticMean(_ numbers: Double...)
  • 支持输入输出参数(In-Out Parameters),这个实际上就是讲值类型转变为引用类型,使用 inout 关键词,例如:swapTwoInts(_ a: inout Int, _ b: inout Int)
  • 另外swift中返回多个值得方式一般使用元组,JS中一般使用返回对象的形式
  • Swift 函数支持 指定参数标签,个人感觉就是参数别名

其余方面基本类似,比如:

  • 支持默认参数值
  • 函数即可以作为参数传入另一个函数,也可以作为返回值
  • 支持嵌套函数
  • 支持闭包

1.多重返回值函数

使用元组让多个值作为一个复合值从函数中返回,返回值可选和TypeScript中语法一致

// 返回类型为 (min: Int, max: Int)? 因为可能返回 nil
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 currentMin < value {
      currentMin = value
    } else if value > currentMax {
      currentMax = value
    }
  }
}

// 这里元组的取值方式 类似于对象 使用点操作
if 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"

2.指定参数标签

// from 是参数标签 hometown 的别名
func greet(person: String, from hometown: String) -> String {
  return "hello \(person)! glad you could visit from \(hometown)"
}

// 调用时
greet(person: "james", from: "china")
// hello james! glad you could visit from china

3.忽略参数标签

使用 _ 可以对参数标签进行忽略

func someFunc(_ firstParam: Int, secondParam: Int) {
  // ...
}

// 调用
someFunc(10, secondParam: 2)

4.默认参数

一般讲默认参数放在最后

func someFunc(param1: Int, param2: Int = 10) {
  // ...
}
// 调用
someFunc(param1: 20)
someFunc(param1: 3, param2: 4)

5.可变参数

可变参数(varriadic parameter) ,一个函数最多只能有一个可变参数

func arithmeticMean(_ numbers: Double...) -> Double {
  var total = 0.0
  for number in numbers {
    total += number
  }
  return total / Double(numbers.count)
}

arithmeticMean(1, 2, 3, 4, 5)
// 返回 3.0, 是这 5 个数的平均数。
arithmeticMean(3, 8.25, 18.75)
// 返回 10.0, 是这 3 个数的平均数。

6.输入输出参数 (In-Out Paramters)

在参数定义前添加 inout 关键词,调用时在实参前添加 & 取地址符关键词。

一个输入输出参数有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。

你只能查到变量给输入输出参数,不能传入常量或者字面量,因为这些量是不能被修改的

// 交换2个数
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
  let temp = a
  a = b
  b = temp
}

// 使用
var inta = 3
var intb = 10
swapTwoInts(&inta, &intb)
// 现在 inta 和 intb的值进行了交换
inta // 10
intb // 3

js中可以使用解构对2个数进行交换:

let a = 3
let b = 10
[b, a] = [a, b]

// 结果
a // 10
b // 3

7.函数作为参数

可以使用 (Int, Int) -> Int 这样的函数类型作为另一个函数的参数类型

func someFunc(_ mathFunc: (Int, Int) -> Int, _ a: Int, _ b: Int) -> Void {
  print("Result: \(mathFunc(a, b))")
}

func addTwoNums(_ a: Int, _ b: Int) -> Int {
  return a + b
}

someFunc(addTwoNums, 10, 20)
// Result: 30

8.函数作为返回值

func stepForward(_ input: Int) -> Int {
  return input + 1
}
func stepBackward(_ input: Int) -> Int {
  return input - 1
}

// 返回类型为 (Int) -> Int
func chooseStepFunc(isBackward: Bool) -> (Int) -> Int {
  return isBackward ? stepBackward : stepBackward
}

// 调用
var currentValue = 3
let move = chooseStepFunc(isBackward: currentValue > 0)
move(10)

func highOrderFunc() -> ((Int, Int) -> Int) {
  return func(_ a: Int, _ b: Int) -> Int{
    return a + b
  }
}

如果学习了后面的闭包,可以将上面例子简化为:

func chooseStepFunc(isBackward: Bool) -> (Int) -> Int {
  return isBackward ? { $0 - 1} : { $0 + 1 }
}
chooseStepFunc(isBackward: 10 > 0)(10)  // 9
chooseStepFunc(isBackward: 10 < 0)(10)  // 11

9.嵌套函数

和JS中一样,默认定义的函数都是全局的,在函数体内定义别的函数称之为嵌套函数

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!
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容