函数

1.多重返回值函数 

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)
}
//需要注意的是,元组的成员不需要在元组从函数中返回时命名,因为它们的名字已经在函数返回类型中指定了。

2.可选元组返回类型 

//注意 可选元组类型如 (Int, Int)? 与元组包含可选类型如 (Int?, Int?) 是不同的.可选的元组类型,整个 元组是可选的,而不只是元组中的每个元素值
func minMax(array: [Int]) -> (min: Int, max: Int)? {
     if array.isEmpty { return nil }
     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)
 }

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

3.函数参数标签和参数名称 

/*
 1.每个函数参数都有一个参数标签(argument label )以及一个参数名称(parameter name)
 2.默认情况下,函数参数使用参数名称来作为它们的参数标签
 3.参数标签在调用函 数的时候使用
 4.调用的时候需要将函数的参数标签写在对应的参数前面
 */

//1.无标签
func someFunction(firstParameterName: Int, secondParameterName: Int) {
    // 在函数体内,firstParameterName 和 secondParameterName 代表参数中的第一个和第二个参数值,也表示默认的参数标签
}
someFunction(firstParameterName: 1, secondParameterName: 2)

//2.有标签
func someFunction(argumentLabel parameterName: Int) {
    // 在函数体内,parameterName 代表参数值
    // argumentLabel代表标签
}
someFunction(argumentLabel: 1)

//3.忽略参数标签
//如果你不希望为某个参数添加一个标签,可以使用一个下划线( _ )来代替一个明确的参数标签
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    
}
someFunction(1, secondParameterName: 2)

4.参数值

//1.默认参数值
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
// 如果你在调用时候不传第二个参数,parameterWithDefault 会值为 12 传入到函数体中。
 }
 someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault = 6
 someFunction(parameterWithoutDefault: 4) // parameterWithDefault = 12
//2.可变参数
//注意:一个函数最多只能拥有一个可变参数
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)
// 返回 3.0, 是这 5 个数的平均数。 
arithmeticMean(3, 8.25, 18.75)
// 返回 10.0, 是这 3 个数的平均数。

5.输入输出参数 ,即是修改函数外的值

/*
 1.注意:输入输出参数不能有默认值,而且可变参数不能用inout标记
 2.调用函数时:参数名前加&符,表示这个值可以被函数修改
 3.输入输出参数和返回值是不一样的。输入输出参数是函数对函数体外产生影响的另一种方式
 */
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)")
// 打印 "someInt is now 107, and anotherInt is now 3"

6.函数类型 

//函数的类型:函数的参数类型和返回类型组成

//类型为:(Int, Int) -> Int
func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}

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

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

//2.函数类型作为参数类型
//函数类型作为另一个函数的参数类型。这样你可以将函数的一部分实现留给 函数的调用者来提供
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// 打印 "Result: 8"

//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
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero 现在指向 stepBackward() 函数。

 

posted @ 2017-03-28 10:49  ZJea  阅读(154)  评论(0编辑  收藏  举报