iOS Developerswift

swift学习 --- 函数

2017-01-17  本文已影响14人  cochn

不带参数的函数

func helloWord() -> String { return "Hello word" }

带一个参数的函数

func helloWord(_ hello:String) -> String { return hello } helloWord("hello world")

带多个参数的函数

func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent); } sayHelloWorld("xiaoming", say: "hello world")

不带返回值的函数

func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent) } sayHelloWorld("xiaoming", say: "hello world")

带返回值得函数

func helloWorld(_ hello: String) ->String{ return hello } let hello = helloWorld("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"

参数

函数参数标签和参数名称

每个函数参数都有一个参数标签( argument label )以及一个参数名称( parameter name )。参数标签在调用函数的时候使用;调用的时候需要将函数的参数标签写在对应的参数前面。参数名称在函数的实现中使用。默认情况下,函数参数使用参数名称来作为它们的参数标签。

参数标签:
  1. 供函数外部使用,多个参数标签可以相同,但是为了方便调用,尽量不要相同。
  2. 可以指定参数标签 下例中perdonName 是指定的参数标签 函数调用时需要显示参数标签
    func sayHelloWorld(perdonName person:String, say sayMsg:String){ print(person + "say" + sayMsg) } sayHelloWorld(perdonName: "xiaoming", say: "hello world")
  3. 参数标签可以隐藏 隐藏的参数标签以 "_" 代替 函数调用时不用显示
    func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent) } sayHelloWorld("xiaoming", say: "hello world")
  4. 如果不指定参数标签 默认参数名称为参数标签
  5. 如果指定了参数标签 在函数调用时必须使用这个标签
参数名称:
  1. 供函数内部使用,并且不能相同。
默认参数值

在函数体中可以为任意一个参数给定一个默认值,当定义默认值之后,在函数调用时可以忽略这个参数。
func xiaomingSayHelloWorld(_ person:String, sayMsg:String = "hello world"){ print(person + "say" + sayMsg) } xiaomingSayHelloWorld("xiaoming") xiaomingSayHelloWorld("xiaoming", sayMsg: "hello")

将不带有默认值的参数放在函数参数列表的最前。一般来说,没有默认值的参数更加的重要,将不带默认值的参数放在最前保证在函数调用时,非默认参数的顺序是一致的,同时也使得相同的函数在不同情况下调用时显得更为清晰。

可变参数

注意:
一个函数最多只能拥有一个可变参数。

输入输出参数

注意:
输入输出参数不能有默认值,而且可变参数不能用 inout 标记。

var a1 = 10 var b1 = 20 func swapTwoInts(_ a: inout Int,_ b: inout Int){ let tempA = a a = b b = tempA } swapTwoInts(&a1, &b1)

注意:
输入输出参数和返回值是不一样的。上面的 swapTwoInts 函数并没有定义任何返回值,但仍然修改了 someInt 和 anotherInt 的值。输入输出参数是函数对函数体外产生影响的另一种方式。

函数类型

func hello(hello: String) ->String{ return hello }
hello函数的函数类型为 (String) -> String

func helloWorld(){ print("hello world") }
helloWorld 的函数类型为 () -> void

函数类型做为函数参数

func hello(hello: String) ->String{ return hello } func helloTwo(_ say: (String) -> String, sayMsg: String){ print(say(sayMsg)) } helloTwo(hello, sayMsg: "hello")

函数类型做为函数返回值

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 现在指向 stepBackward() 函数。

嵌套函数

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!

参考文献

The Swift Programming Language 中文版 wiki - 函数

上一篇下一篇

猜你喜欢

热点阅读