美文网首页iOS Developerswift
swift学习 --- 函数

swift学习 --- 函数

作者: cochn | 来源:发表于2017-01-17 14:55 被阅读14次

    不带参数的函数

    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")

    • 函数调用时如果忽略这个参数,其会将默认值传入函数内。
    • 函数调用时如果没有忽略这个参数,其将会忽略默认的值,传入调用时传入的值

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

    可变参数
    • 通过在变量类型名后面加入(...)的方式来定义可变参数
    • 一个可变参数可以接受零个多个参数
    • 函数调用时,用可变参数来指定函数参数可以被传入不确定数量的输入值
      func xiaomingSay(_ sayMsg:String...) -> String{ var sayMessage:String = "" for msg in sayMsg {sayMessage = sayMessage + "" + msg + " "} return sayMessage } let say = xiaomingSay("今天是1月17号","还有几天就可以回家啦")

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

    输入输出参数
    • 函数的参数默认是常量,如果想要修改函数参数的值,并且想要这些修改在函数调用之后仍然存在,那么需要把这个参数定义为输入输出函数
    • 定义一个输入输出参数时,在参数定义前加 inout 关键字。
    • 一个输入输出参数有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。
    • 只能传递变量给输入输出参数。不能传入常量或者字面量,因为这些量是不能被修改的。当传入的参数作为输入输出参数时,需要在参数名前加 & 符,表示这个值可以被函数修改。

    注意:
    输入输出参数不能有默认值,而且可变参数不能用 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 - 函数

    相关文章

      网友评论

        本文标题:swift学习 --- 函数

        本文链接:https://www.haomeiwen.com/subject/aglmbttx.html