05-Swift函数

2020-12-12  本文已影响0人  一抹相思泪成雨

1.函数的定义

func sum(v1: Int, v2: Int) -> Int {
    return v1 + v2
}
sum(v1: 10, v2: 20)

/// 无返回值 等价
func sayHello1() -> Void {}
func sayHello2() -> () {}
func sayHello3() {}
1.2隐式返回
func add(v1: Int, v2: Int) -> Int {
    v1 + v2
}
add(v1: 10, v2: 20)
1.3返回元组,实现多返回值
func calculate(v1: Int, v2: Int) -> (sum: Int, difference: Int, average: Int) {
    let sum = v1 + v2  // 大于两句 添加return
    return (sum, v1-v2, sum>>1)
}

let result = calculate(v1: 20, v2: 10)
result.sum
result.difference
result.average

2.文档注释

/// 求和【概述】 ///
/// 将2个整数相加【更详细的描述】 ///
/// - Parameter v1: 第1个整数
/// - Parameter v2: 第2个整数
/// - Returns: 2个整数的和 ///
/// - Note:传入2个整数即可【批注】
///
func sum(v1: Int, v2: Int) -> Int {
  v1 + v2 }

3.参数标签

 func goToWork(at time: String) {
    print("this time is \(time)")
}
goToWork(at: "08:00")

func sum(_ v1: Int, _ v2: Int) -> Int {
     v1 + v2
}
sum(10, 20)

4.默认参数值

func check(name: String = "nobody", age: Int, job: String = "none") {
    print("name \(name), age = \(age), job = \(job)")
}
check(name: "Jack", age: 20, job: "Doctor")
check(name: "Rose", age: 18)
check(age: 10, job: "Batman")
check(age: 15)

// 这里的middle不可以省略参数标签
func test(_ first: Int = 10, middle: Int, _ last: Int = 30) { } 
test(middle: 20)
 

5.可变参数Int...

func sum(_ numbers: Int...) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}
sum(10, 20, 30, 40) // 100

func test(_ numbers: Int..., string: String, _ other: String) {}
test(10, 20, 30, string: "Jack", "Rose")

6.Swift自带的print函数可变参数

// 可变参数  拼接符 “”  并换行
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n")
print(1, 2, 3, 4, 5, separator: "_") // 1_2_3_4_5 
print("My name is Jake.", terminator: "") // 不再换行

7.输入输出参数

//可以用inout定义一个输入输出参数:可以在函数内部修改外部实参的值
func swapValues(_ v1: inout Int, _ v2: inout Int) {
//    let tmp = v1
//    v1 = v2
//    v2 = tmp
    (v1, v2) = (v2, v1)
}
var num1 = 10
var num2 = 20
swapValues(&num1, &num2)

8.函数重载

func testSum(v1: Int, v2: Int) -> Int {
    v1 + v2
}
func testSum(v1: Int, v2: Int, v3: Int) -> Int {
    v1 + v2 + v3
}/// 参数个数不同
func testSum(v1: Int, v2: Double) -> Double {
    Double(v1) + v2
}/// 参数类型不同
func testSum(_ v1: Int, _ v2: Int) -> Int {
    v1 + v2 + 1
}/// 参数标签不同

print(testSum(v1: 10, v2: 20))
print(testSum(v1: 10, v2: 20, v3: 30))
print(testSum(v1: 10, v2: 20.0))
print(testSum(10, 20))

/// 1、返回值类型与函数重载无关
func testSum1(v1: Int, v2: Int) -> Int { v1 + v2 }
func testSum1(v1: Int, v2: Int) {}
//testSum1(v1: 10, v2: 20)

/// 2、默认参数值和函数重载一起使用产生二义性时,编译器不会报错、 C++中会报错
func testSum2(v1: Int, v2: Int) -> Int { v1 + v2 }
func testSum2(v1: Int, v2: Int, v3: Int = 10) -> Int { v1 + v2 + v3 }
/// 会调用上面的
testSum2(v1: 10, v2: 20)

/// 3、可变参数、省略参数标签,函数重载一起使用产生二义性时,编译器有可能会报错
func testSum3(_ v1: Int, _ v2: Int) -> Int {
    print("+")
    return v1 + v2
}
func testSum3(_ numbers: Int...) -> Int {
    print("numbers")
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}

9.内联函数

 // 永远不会被内联(即使开启了编译器优化) @inline(never) func test() {
    print("test")
}
 // 开启编译器优化后,即使代码很长,也会被内联(递归调用函数、动态派发的函数除外) @inline(__always) func test() {
    print("test")
}

10.函数类型

 func test() { } // () -> Void 或者 () -> ()

 func sum(a: Int, b: Int) -> Int {
     a+b
} // (Int, Int) -> Int

 // 定义变量
var fn: (Int, Int) -> Int = sum
fn(2, 3) // 5,调用时不需要参数标签

11.函数类型作为函数参数

func next(_ input: Int) -> Int { input + 1}
func previous(_ input: Int) -> Int { input - 1 }
func forward(_ forward: Bool) -> (Int) -> Int {
    forward ? next : previous
}
forward(true)(3)
forward(false)(3)
typealias
// 日期
typealias Date = (year: Int, month: Int, day: Int) func test(_ date: Date) {
print(date.0)
print(date.year) }
test((2011, 9, 10))

// 别名设置
typealias IntFn = (Int, Int) -> Int
func difference(v1: Int, v2: Int) -> Int { v1 - v2 }
let fn: IntFn = difference
fn(20, 10)

func setFn(_ fn: IntFn) { } setFn(difference)
func getFn() -> IntFn { difference }

12.嵌套函数

func forward1(_ result: Bool) -> (Int) -> Int {
    func next(_ input: Int) -> Int {
        input + 1
    }
    func previous(_ input: Int) -> Int {
        input - 1
    }
    return result ? next : previous
}

forward1(true)(3)
forward1(false)(3)
上一篇 下一篇

猜你喜欢

热点阅读