美文网首页
函数-Functions [swift 5.1]

函数-Functions [swift 5.1]

作者: 阿狸小朋友 | 来源:发表于2019-06-15 16:58 被阅读0次

    Functions(函数)Methods(方法)这两个概念我觉得差别并不大,都是完成特定任务的独立代码片段。
    我的理解是函数的范围更广,方法是属于对象的范畴,如实例方法,类方法。
    因为docs.swift.org方法函数区别说明,所以我在学习时也区别对待。此章节为函数的总结。

    1. 定义和调用
    func greet(person: String) -> String {
        let greeting = "Hello, " + person + "!"
        return greeting
    }
    
    print(greet(person: "Anna"))
    
    1. 多参数方法
    func greet(person: String, alreadyGreeted: Bool) -> String {
        if alreadyGreeted {
            return greetAgain(person: person)
        } else {
            return greet(person: person)
        }
    }
    print(greet(person: "Tim", alreadyGreeted: true))
    // Prints "Hello again, Tim!"
    
    1. 没有返回值的方法是返回void,这是空元组,写成()

    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)
    }
    
    1. 参数标签和参数名称
    5.1 带参数标签的
    func someFunction(argumentLabel parameterName: Int) {
        // In the function body, parameterName refers to the argument value
        // for that parameter.
    }
    
    5.2 不带参数标签的,当没有参数标签时,参数名称为参数标签(默认)
    func someFunction(parameterName: Int) {
        // In the function body, parameterName refers to the argument value
        // for that parameter.
    }
    
    5.3 调用时为了省略参数标签,则需要在方法参数名前加`_`
    func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
        // In the function body, firstParameterName and secondParameterName
        // refer to the argument values for the first and second parameters.
    }
    someFunction(1, secondParameterName: 2)
    
    1. 默认参数
    带有默认参数时,调用时可以省略该参数
    func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
        // If you omit the second argument when calling this function, then
        // the value of parameterWithDefault is 12 inside the function body.
    }
    someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12
    
    1. 可变参数
    使用`...`代表多个参数
    func arithmeticMean(_ numbers: Double...) -> Double {
        var total: Double = 0
        for number in numbers {
            total += number
        }
        return total / Double(numbers.count)
    }
    
    1. inout参数
    相当于传入的变量指针(地址)
    func swapTwoInts(_ a: inout Int, _ b: inout Int) {
        let temporaryA = a
        a = b
        b = temporaryA
    }
    
    var someInt = 3
    var anotherInt = 107
    swapTwoInts(&someInt, &anotherInt)
    print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
    // Prints "someInt is now 107, and anotherInt is now 3"
    
    
    1. 函数类型的参数和返回值
    func addTwoInts(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
    func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
        return a * b
    }
    
    9.1 定义一个名为的变量mathFunction,它的类型为'一个带两个Int值的函数,并返回一个Int值'。设置这个新变量来引用被调用的函数addTwoInts。
    var mathFunction: (Int, Int) -> Int = addTwoInts
    
    9.2 函数类型作为参数类型
    func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
        print("Result: \(mathFunction(a, b))")
    }
    
    9.3 函数类型作为返回类型
    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
    }
    
    9.4 嵌套函数
    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
    }
    

    相关文章

      网友评论

          本文标题:函数-Functions [swift 5.1]

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