Swift函数学习

来源:互联网 发布:张国荣过誉 知乎 编辑:程序博客网 时间:2024/05/16 12:21
func someFunction(firstParameterName: Int, secondParameterName: Int) {    // In the function body, firstParameterName and secondParameterName    // refer to the argument values for the first and second parameters.}

1.每个函数都用 func开头标示,someFunction为函数名,firstParameterName和secondParameterName为实际参数标签,
(firstParameterName: Int, secondParameterName: Int)为形式参数。
2.每一个函数的形式参数都包含实际参数标签和形式参数名。实际参数标签用在调用函数的时候;在调用函数的时候每一个实际参数前边都要写实际参数标签。形式参数名用在函数的实现当中。默认情况下,形式参数使用它们的形式参数名作为实际参数标签,即实际参数标签通常是被默认的,也可以指定一个新的标签,如3。
3.在提供形式参数名之前写实际参数标签,用空格分隔:

func someFunction(argumentLabel parameterName: Int) {    // In the function body, parameterName refers to the argument value    // for that parameter.}

上面argumentLabel为指定的实际参数标签,在调用函数的时候变成

someFunction(argumentLabel 10)

4.省略实际参数标签,如果对于函数的形式参数不想使用实际参数标签的话,可以利用下划线( _ )来为这个形式参数代替显式的实际参数标签。

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)

5.默认形式参数值,你可以通过在形式参数类型后给形式参数赋一个值来给函数的任意形式参数定义一个默认值。如果定义了默认值,你就可以在调用函数时候省略这个形式参数

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

6.可变形式参数,一个可变形式参数可以接受零或者多个特定类型的值。当调用函数的时候你可以利用可变形式参数来声明形式参数可以被传入值的数量是可变的。可以通过在形式参数的类型名称后边插入三个点符号( …)来书写可变形式参数。

传入到可变参数中的值在函数的主体中被当作是对应类型的数组。举个栗子,一个可变参数的名字是 numbers类型是 Double…在函数的主体中它会被当作名字是 numbers 类型是 [Double]的常量数组。
下面的栗子计算了一组任意长度的数字的算术平均值(也叫做平均数)。

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 numbersarithmeticMean(3, 8.25, 18.75)// returns 10.0, which is the arithmetic mean of these three numbers

7.就像上面描述的,可变形式参数只能在函数的内部做改变。如果你想函数能够修改一个形式参数的值,而且你想这些改变在函数结束之后依然生效,那么就需要将形式参数定义为输入输出形式参数。

在形式参数定义开始的时候在前边添加一个 inout关键字可以定义一个输入输出形式参数。输入输出形式参数有一个能输入给函数的值,函数能对其进行修改,还能输出到函数外边替换原来的值。

你只能把变量作为输入输出形式参数的实际参数。你不能用常量或者字面量作为实际参数,因为常量和字面量不能修改。在将变量作为实际参数传递给输入输出形式参数的时候,直接在它前边添加一个和符合 ( &) 来明确可以被函数修改。

这里有一个 swapTwoInts(::)函数,它有两个输入输出整数形式参数 a和 b:

func swapTwoInts(_ a: inout Int, _ b: inout Int) {    let temporaryA = a    a = b    b = temporaryA}

函数 swapTwoInts(::)只是简单的将 b和 a的值进行了调换。函数将 a的值储存在临时常量 temporaryA中,将 b的值赋给 a,然后再将 temporaryA的值赋给 b。

你可以通过两个 Int类型的变量来调用函数 swapTwoInts(::)去调换它们两个的值,需要注意的是 someInt的值和 anotherInt的值在传入函数 swapTwoInts(::)时都添加了和符号。

var someInt = 3var anotherInt = 107swapTwoInts(&someInt, &anotherInt)print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")// prints "someInt is now 107, and anotherInt is now 3"

8.使用函数类型
你可以像使用 Swift 中的其他类型一样使用函数类型。例如,你可以给一个常量或变量定义一个函数类型,并且为变量指定一个相应的函数。

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