Swift5.2-函数

2020-09-02  本文已影响0人  Gomu_iOS

一、声明

二、函数

//: 定义函数:定义函数的名称+定义函数的参数+定义函数的返回值。
func test(name: String) -> String
{
    return "welcome " + name
}

//: 调用函数:使用其名称调用该函数,传递与函数参数类型匹配的值。
print(test(name: "小明")) //: 打印:welcome 小明
func test() -> String
{
    return "welcome"
}
func test(name: String, age: Int) -> String
{
    return "welcome \(name) age: \(age)"
}
print(test(name: "小明",age: 14)) //: 打印: welcome 小明 age: 14
func test(name: String, age: Int)
{
   print("welcome \(name) age: \(age)")
}
//: 或
func test(name: String, age: Int) -> Void
{
   print("welcome \(name) age: \(age)")
}

使用元组类型作为函数的返回值类型,同时返回多个函数值。

//: 定义
func minMax(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array {
        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

若函数返回的元组类型可能为空值时,则可以使用可选的元组返回类型。通过在元组类型的右括号后面放置一个?来编写一个可选的元组返回类型,例如(Int,Int)?(String,Int,Bool)?

func minMax(array: [Int]) -> (min: Int, max: Int)? {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

//: 使用可选绑定来检查minMax(array :)函数是否返回nil,这样使用返回值更严谨
if let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) {
    print("min is \(bounds.min) and max is \(bounds.max)")
}
//: 标签函数
func tagFunction(argumentLabel parameterName: Int) {
}
//: 调用
tagFunction(tag: 11)
    
//: 默认情况
func origFunction(name: Int) {
}
origFunction(name: 11)
func tagFunction(_ name: Int) {
}
tagFunction(14)

通过在该参数的类型之后为参数赋值来为函数中的任何参数定义默认值。如果定义了默认值,则可以在调用函数时省略该参数。

func defalutValue(value1: Int, value2: Int = 12) {
   print("\(value1)  \(value2)")
}
//:调用,两者皆可以
defalutValue(value1: 1, value2: 5) //: 打印 1 5
//: 因为某个参数值定义了默认值,因而多出下面的方法。
defalutValue(value1: 2) //: 打印 2 12

函数中使用可变参数,表示在函数调用时可以向该参数传递特定类型的不同数量的参数值。可变参数接受零或多个指定类型的值。

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {//: numbers 作为[Double]类型的数组存在
        total += number
    }
    return total / Double(numbers.count)
}
//: 调用
let average = arithmeticMean(1,2,3,4)
print("平均数\(average)")//!< 平均数2.5
//: 根据内存地址交换两个整型数值
func changeTwoIntNumbers(_ a:inout Int,_ b:inout Int) {
    let temp = a
    a = b
    b = temp
}
//: 使用时需注意不能将类型为'Int'的不可变值作为inout参数传递,否则会报错
var a = 6
var b = 7
changeTwoIntNumbers(&a, &b)
print("b:\(b),a:\(a)")//: b:6,a:7

使用函数类型如(Int,Int)- > Int作为另一个函数的参数类型。

func addFuction(fuction:(Int,Int) -> Int, a: Int, b: Int){
   print("\(fuction(a, b))")
}
    
func add(a: Int, b: Int) -> Int {
   return a + b
}
//: 调用
addFuction(fuction: add(a:b:), a: 4, b: 7) //: 输出 11
func stepBackward(_ input: Int) -> Int {
    return input - 1
}

//: 定义返回值类型为函数类型的函数
func chooseStepFunction() -> (Int) -> Int {
    return stepBackward
}

//: 使用
var currentValue = 3
let tempChooseStepFunction = chooseStepFunction()
while currentValue > 0 {
    currentValue = tempChooseStepFunction(currentValue)
    print(currentValue)
}//: 2 1 0
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    func stepBackward(_ input: Int) -> Int {
        return input - 1
    }
    func stepForward(input: Int) -> Int {
        return input + 1
    }
    return backward ? stepBackward : stepForward
}
上一篇 下一篇

猜你喜欢

热点阅读