zoukankan      html  css  js  c++  java
  • 【Swift学习】Swift编程之旅---函数(十)

      函数是一组用于执行特定任务的独立的代码段,你用一个名字来标识函数,这个名字是用来“调用”函数来执行它的任务。

      swift统一函数的语法具有足够的灵活性来表达任何一个简单的不带参数的名称与本地和外部的每个参数的参数名称的复杂objective-c-style C风格的函数方法。参数可以提供默认值,以简化函数调用,并且可以通过在输出参数中,一旦该功能完成了它的执行,它就可以修改传递的变量

      swift每一个函数都有一个类型,由形参类型和返回类型组成。你可以使用这种类型的任何其他类型,这使得它很容易将函数作为参数的其他函数,并返回函数的函数。函数还可以用其他函数来封装在嵌套函数作用域内的有用函数。

      定义和调用函数

    func sayHello(personName: String) -> String {
        let greeting = "Hello, " + personName + "!"
        return greeting
    }
    print(sayHello("Anna"))
    // Prints "Hello, Anna!"
    print(sayHello("Brian"))
    // Prints "Hello, Brian!

    sayHello是函数的名称,personName是它的参数,类型是String,返回一个String类型的数据,使用->符号来标识函数的返回类型,后面是函数的返回类型。

      函数的形参和返回值

      函数参数和返回值是非常灵活的。您可以从一个简单的实用程序函数中定义任何一个简单的函数,用一个未命名的参数来定义一个复杂的函数。

      无参函数

    func sayHelloWorld() -> String {
        return "hello, world"
    }
    print(sayHelloWorld())
    // Prints "hello, world

      多参函数

    func sayHello(personName: String, alreadyGreeted: Bool) -> String {
        if alreadyGreeted {
            return sayHelloAgain(personName)
        } else {
            return sayHello(personName)
        }
    }
    print(sayHello("Tim", alreadyGreeted: true))
    // Prints "Hello again, Tim!

      

      无返回值函数(实际返回void)

    func sayGoodbye(personName: String) {
        print("Goodbye, (personName)!")
    }
    sayGoodbye("Dave")
    // Prints "Goodbye, Dave!

      

    func printAndCount(stringToPrint: String) -> Int {
        print(stringToPrint)
        return stringToPrint.characters.count
    }
    func printWithoutCounting(stringToPrint: String) {
        printAndCount(stringToPrint)
    }
    printAndCount("hello, world")
    // prints "hello, world" and returns a value of 12
    printWithoutCounting("hello, world")
    // prints "hello, world" but does not return a value

      多返回值函数

    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([8, -6, 2, 109, 3, 71])
    print("min is (bounds.min) and max is (bounds.max)")
    // Prints "min is -6 and max is 109

       函数形参名

       函数的形参包括本地和外部参数名。外部参数名称用于将参数传递给函数调用。函数的实现中使用了局部参数名称

    func someFunction(firstParameterName: Int, secondParameterName: Int) {
        // function body goes here
        // firstParameterName and secondParameterName refer to
        // the argument values for the first and second parameters
    }
    someFunction(1, secondParameterName: 2)

      默认形参值

      您可以在参数类型之后给任何的参数设置一个默认值,如果您设置了默认值,那么就可以在调用该函数时忽略此参数。

    func someFunction(parameterWithDefault: Int = 12) {
        // function body goes here
        // if no arguments are passed to the function call,
        // value of parameterWithDefault is 12
    }
    someFunction(6) // parameterWithDefault is 6
    someFunction() // parameterWithDefault is 12

    在函数的参数列表的结尾处设置默认值

      可变参数

      一个可变的参数接受特定类型的零个或多个值。当函数被调用时,你使用一个可变的参数可以传入不同数量的指定类型的参数,在参数类型名称后面插入...来实现可变参数。函数体的可变参数值被当做一个特定类型的数组来处理。例子如下

    func arithmeticMean(numbers: Double...) -> Double {
        var total: Double = 0
        for number in numbers {
            total += number
        }
        return total / Double(numbers.count)
    }
    arithmeticMean(1, 2, 3, 4, 5)
    // returns 3.0, which is the arithmetic mean of these five numbers
    arithmeticMean(3, 8.25, 18.75)
    // returns 10.0, which is the arithmetic mean of these three numbers

      numbers: Double...被当做一个存放Double类型的数组,我们可以遍历它的每一个元素

      注意:一个函数只能有一个可变参数

      In-Out 形参

      函数参数默认为常量,在函数体内修改参数值会发生编译时错误,这意味着你不能改变一个参数的值。如果你想要一个函数来修改某个参数的值,并希望这些改变在函数调用结束后是永久的,那么使用 in-out参数来代替

      使用inout关键字来定义一个in-out参数,但是参数不能为常量,只能为变量。当你调用时直接在参数名前加&符号来标识此参数可以被该函数修改。

    func swapTwoInts(inout a: Int, inout _ b: 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”

      函数类型

      每个函数都有一个特定的函数类型,由形参类型和函数的返回类型组成

    func addTwoInts(a: Int, _ b: Int) -> Int {
        return a + b
    }
    func multiplyTwoInts(a: Int, _ b: Int) -> Int {
        return a * b
    }

     这个例子定义了addTwoInts,multiplyTwoInts2个函数,它们的类型都是(Int, Int) -> Int

    func printHelloWorld() {
        print("hello, world")
    }

    它的类型是()->Void

      函数类型的使用

      在swift中您可以像任何其他类型一样的使用函数类型。例如,你可以定义一个常量或变量为一个函数类型,并为变量指定一个对应的函数:

    var mathFunction: (Int, Int) -> Int = addTwoInts

     可以解读为:"定义一个名为mathFunction变量,该变量的类型为'一个函数,它接受两个Int值,并返回一个Int值。'设置这个新的变量来引用名为addTwoInts函数。

    该addTwoInts函数具有与mathFunction相同类型的变量,所以这个赋值能通过swift的类型检查。

    现在你可以使用mathFunction来调用指定的函数:

    print("Result: (mathFunction(2, 3))")
    // Prints "Result: 5

    与其他类型一样,当你给函数赋一个常量或者变量时,你可以让Swift去推断函数的类型。    

    let anotherMathFunction = addTwoInts
    // anotherMathFunction is inferred to be of type (Int, Int) -> Int

      作为形参类型的函数类型

      可以使用一个函数类型,如(Int, Int)->Int作为另一个函数的形参类型。这使你预留了一个函数的某些方面的函数实现,让调用者提供的函数时被调用。

    func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
        print("Result: (mathFunction(a, b))")
    }
    printMathResult(addTwoInts, 3, 5)
    // Prints "Result: 8

      

      作为返回类型的函数类型 

      你可以将一个函数类型作为另一个函数的返回类型。你可以在返回函数的返回箭头(->) 后立即编写一个完整的函数类型来实现。下面的例子定义了两个简单的函数调用stepForward和stepBackward。stepForward函数返回一个输入值+1的结果,而stepBackward函数返回一个输入值-1的结果。这两个函数都有一个相同的(Int) -> Int类型 :

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

    func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
        return backwards ? stepBackward : stepForward
    }
    var currentValue = 3
    let moveNearerToZero = chooseStepFunction(currentValue > 0)
    // moveNearerToZero now refers to the stepBackward() function
    print("Counting to zero:")
    // Counting to zero:
    while currentValue != 0 {
        print("(currentValue)... ")
        currentValue = moveNearerToZero(currentValue)
    }
    print("zero!")
    // 3...
    // 2...
    // 1...
    // zero!

      嵌套函数

      迄今为止所有你在本章中遇到函数都是全局函数,在全局作用域中定义。其实你还可以在其他函数体中定义函数,被称为嵌套函数。嵌套函数默认对外界是隐藏的,但仍然可以通过它们包裹的函数调用和使用它。enclosing function也可以返回一个嵌套函数,以便在其他作用域中使用嵌套函数。你可以重写上面的chooseStepFunction例子使用并返回嵌套函数:

    func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
        func stepForward(input: Int) -> Int { return input + 1 }
        func stepBackward(input: Int) -> Int { return input - 1 }
        return backwards ? stepBackward : stepForward
    }
    var currentValue = -4
    let moveNearerToZero = chooseStepFunction(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!
  • 相关阅读:
    Teamplate Workflow Architecture(Teamplate工作流架构)
    Las Vegas拉斯维加斯(赌城)游记
    初步尝试Teamplate Workflow Web Part for SharePoint Portal Server
    灵活管理Remote Objects生存期(lifetime)
    Wincv.exe类查看器工具(Class Viewer)
    重构(Refactoring)技巧读书笔记 之二
    重构(Refactoring)技巧读书笔记 之一
    尝试RemotingSqlHelper的若干问题
    关于ADO.Net连接池(Connection Pool)的一些个人见解
    SQL Server 最佳实践分析器使用小结
  • 原文地址:https://www.cnblogs.com/salam/p/5410984.html
Copyright © 2011-2022 走看看