#6 函数

2018-08-24  本文已影响10人  JamesSawyer

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

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

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!
上一篇 下一篇

猜你喜欢

热点阅读