Swift5.0笔记 - 3. 函数

2020-10-27  本文已影响0人  南城同學

1. 格式

1). 无返回值格式

func sayHello() -> Void {
    print("Hello")
}

func sayHello() ->() {
    print("Hello")
}

func sayHello() {
    print("Hello")
}

2). 有返回值格式

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

3). 隐式返回

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

4). 返回元组:实现返回值

func calculate(v1: Int,v2: Int) ->(sum:Int, difference: Int, average:Int) {
    let sum = v1 + v2
    return(sum, v1 - v2,sum >> 1)
}
let result = calculate(v1: 20, v2: 10)

result.sum //30
result.difference //10
result.average //15

2. 函数的文档注释

/// 求和【概述】
///
/// 将两个数相加【更详细的描述】
///
/// - Parameter v1: 第1个整数
/// - Parameter v2: 第2个整数
/// - Returns: 2个整数之和
///
/// - Note:传入2个整数即可【批注】
///

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


3. 参数标签

// "at" 用来保证外面调用读起来比较通顺 : 称为 “标签”
// "time" 用来保证你能理解传进来的参数的意思 : 称为“形参”
//
func goToWork(at time: String) {
    print("this time is \(time)")
}
goToWork(at: "08:00")
// this time is 08:00
func sum03(_ v1: Int, _ v2: Int) ->Int {v1 + v2}
sum03(10, 20)

4. 默认参数值

func check(name: String = "noboday", age:Int, job: String = "none") {
    print("name = \(name), age = \(age), job = \(job)")
}
check(age: 15)

5. 可变参数

func sum04(_ numbers: Int...) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}
sum04(10, 20, 30, 40) //100
func test04(_ numbers: Int..., string01: String, _ other: String) {}
test04(10, 20, 30, string01: "Jack","Rose")

6. Swift自带的print函数

/// - Parameters:
///   - items: Zero or more items to print.
///   - separator: A string to print between each item. The default is a single
///     space (`" "`).
///   - terminator: The string to print after all items have been printed. The
///     default is a newline (`"\n"`).
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n")

7. 输入输出参数

var number05 = 10
func add05(_ num: inout Int) {
    num += 1
}
add05(&number05)

注意点:

var number05 = 10
func add05(_ num: inout Int) {
    num += 1
}
add05(&number05)
add05(&40) // 这种是错误的❌

8. 函数重载(Function Overload)

规则:

// 以下函数在同一个作用域内都是可以调用成功的;相当于可区分成不同的函数;
func sumFun(v1: Int, v2: Int) -> Int {
    v1 + v2
}

func sumFun(v1: Int, v2: Int, v3: Int) -> Int {
    v1 + v2 + v3
} //参数个数不同

func sumFun(v1: Int, v2: Double) -> Double {
    Double(v1) + v2
} //参数类型不同

func sumFun(_ v1: Int, _ v2: Int) -> Int {
    v1 + v2
} //参数标签不同

func sumFun(a: Int, b: Int) -> Int {
    a + b
}//参数标签不同

注意点:

func sumFun(v1: Int, v2: Int) -> Int {
    v1 + v2
}

func sumFun(v1: Int, v2: Int, v3: Int = 10) -> Int {
    v1 + v2 + v3
}
//会调用sumFun(v1: Int, v2: Int)
sumFun(v1: 10, v2: 20)

9. 内联函数(Inline Function)

1) 如果开启了编译器优化(Release模式默认会开启优化),编译器会自动将某些函数变成内联函数;

2) 哪些函数不会被自动内联?

3) @inline

@inline(never) func test() {
    print("test")
}
@inline(__always) func test() {
     print("test")
}

10. 函数类型(Function Type)


func test03(){} // 函数类型 ()->Void 或者 ()->()
func sum04(a: Int, b: Int) -> Int {
    a + b
} //函数类型:(Int, Int)-> Int

var fn: (Int, Int) -> Int = sum04

fn(10, 20) // 50,调用时不需要参数标签

1)函数类型作为参数

//函数类型作为参数
func sum05(v1: Int, v2: Int) ->Int {
    v1 + v2
}

func difference02(v1: Int, v2: Int) -> Int {
    v1 - v2
}

func printfResult(_ mathFn: (Int, Int) -> Int, _ a: Int, _ b: Int){
    print("Result:\(mathFn(a, b))")
}

printfResult(sum05, 5, 2) //7
printfResult(difference02, 5, 2)//3

2)函数类型作为返回值

func next(_ input: Int) -> Int {
    input + 1
}

func previous(_ input: Int) -> Int {
    input - 1
}

func forword(_ forword: Bool) -> (Int) -> Int { // (Int) -> Int 函数类型
    forword ? next : previous
}
forword(true)(3) //4
forword(false)(3) //2


11. typealias

  1. Swift中是没有ByteShortLong这三个类型的
typealias Byte = Int8 //1个字节
typealias Short = Int16 //2个字节
typealias Long = Int64 // 8个字节
  1. 给元组起别名
typealias Date = (year: Int, month: Int, day: Int)
func test06(_ date: Date) {
    print(date.0)
    print(date.year)
}
test06((2020, 9, 10))
  1. 给函数类型起别名
typealias IntFun = (Int, Int) -> Int

func difference03(v1: Int, v2: Int) -> Int {
    v1 - v2
}

let fnTest: IntFun = difference03
fnTest(20, 10) // 10

func setFn(_ fn: IntFun) { }
setFn(difference03)
func getFun() -> IntFun { difference03 }

12. 嵌套函数(Nested Function)

func forward(_ forward:Bool) -> (Int)->Int{
    func next(_ input:Int) -> Int {
        input+1
    }
    func previous(_ input:Int) -> Int{
        input-1
    }
        return forward ? next : previous
}
forward(true)(3)//4

forward(false)(3)//2
上一篇下一篇

猜你喜欢

热点阅读