函数乡相关知识点
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
//: [Previous](@previous)
|
//: [Previous](@previous)
|
||||||
|
|
||||||
|
import Darwin
|
||||||
|
import Foundation
|
||||||
|
|
||||||
//: ## 函数
|
//: ## 函数
|
||||||
/*:
|
/*:
|
||||||
函数的定义:
|
函数的定义:
|
||||||
@@ -35,14 +38,185 @@ result.sum
|
|||||||
//: ### 函数文档注释
|
//: ### 函数文档注释
|
||||||
/// 求和
|
/// 求和
|
||||||
///
|
///
|
||||||
|
/// -Parameters v1
|
||||||
///
|
///
|
||||||
func addNumber(v1: Int, v2: Int) -> Int {
|
func addNumber(v1: Int, v2: Int) -> Int {
|
||||||
v1 + v2
|
v1 + v2
|
||||||
}
|
}
|
||||||
|
|
||||||
addNumer(v1: 20, v2: 30);
|
//addNumer(v1: 20, v2: 30);
|
||||||
|
|
||||||
//: ###参数标签
|
//: ###参数标签
|
||||||
|
// 参数名称的别名,如下发 at 相当与time的别名
|
||||||
|
// 可以使用_来忽略标签名
|
||||||
|
func goToWork(at time: String) {
|
||||||
|
print("this time is \(time)")
|
||||||
|
}
|
||||||
|
|
||||||
|
goToWork(at: "08:00")
|
||||||
|
|
||||||
|
// 参数的默认值
|
||||||
|
// name是字符串类型,默认值是
|
||||||
|
func check(name: String = "nobody", age: Int, job: String = "none") {
|
||||||
|
print("name = \(name), age = \(age), job = \(job)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在省略标签的时候,需要注意避免出错
|
||||||
|
// 标签不能全部省略
|
||||||
|
func test(_ first: Int = 10, middle: Int, _ last: Int = 30) {}
|
||||||
|
|
||||||
|
// 可变参数
|
||||||
|
/*:
|
||||||
|
可变参数:
|
||||||
|
- 一个函数最多有1个可变参数
|
||||||
|
- 紧跟在可变参数后面的参数不能省略参数标签
|
||||||
|
*/
|
||||||
|
func sum(_ numbers: Int...) -> Int {
|
||||||
|
var total = 0
|
||||||
|
for number in numbers {
|
||||||
|
total += number
|
||||||
|
}
|
||||||
|
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
// str的参数标签不能省略
|
||||||
|
func test(_numbers: Int..., str: String, _ other: String) {}
|
||||||
|
|
||||||
|
|
||||||
|
// swift自带的print函数
|
||||||
|
//public func print(<#T##Any#>, separator: <#T##String#>, terminator: <#T##String#>)
|
||||||
|
|
||||||
|
//: ####输入输出参数(In-Out Parameter)
|
||||||
|
/*:
|
||||||
|
可以用inout定义一个输入输出参数:可以在函数内部修改外部实参的值
|
||||||
|
- 可变参数不能标记为inout
|
||||||
|
- inout参数不能由默认值
|
||||||
|
- inout参数的本质是地址传递(引用传递)
|
||||||
|
- inout参数只能传入可以被多次赋值的
|
||||||
|
*/
|
||||||
|
// 可以用inout定义一个输入输出参数:可以在函数内部修改外部实参的值
|
||||||
|
// 可变参数不能标记为inout
|
||||||
|
var number = 10
|
||||||
|
func add(_ num: inout Int) {
|
||||||
|
num += 20
|
||||||
|
}
|
||||||
|
add(&number)
|
||||||
|
print(number)
|
||||||
|
|
||||||
|
//: ### 函数重载(Function Overload)
|
||||||
|
/*:
|
||||||
|
规则:
|
||||||
|
- 函数名相同
|
||||||
|
- 参数个数不同 || 参数类型不同 || 参数标签不同
|
||||||
|
|
||||||
|
注意点:
|
||||||
|
- 返回值类型与函数重载无关
|
||||||
|
- 默认参数值和函数重载一起使用产生二义时,编译器并不会报错
|
||||||
|
- 可变参数、省略参数标签、函数重载一起使用产生二义时,编译器会报错
|
||||||
|
*/
|
||||||
|
|
||||||
|
func sum(v1: Int, v2: Int) -> Int {}
|
||||||
|
func sum(v1: Int, v2: Int, v3: Int) -> Int {} // 参数个数不同
|
||||||
|
func sum(_ v1: Int, _ v2: Int) -> Int {} // 参数标签不同
|
||||||
|
|
||||||
|
// 默认参数值和函数重载一起使用产生二义时,编译器并不会报错
|
||||||
|
func sum(v1: Int, v2: Int) -> Int {
|
||||||
|
v1 + v2
|
||||||
|
}
|
||||||
|
|
||||||
|
func sum(v1: Int, v2: Int, v3: Int) -> Int {
|
||||||
|
v1 + v2 + v3
|
||||||
|
}
|
||||||
|
|
||||||
|
sum(v1: 10, v2: 20)
|
||||||
|
|
||||||
|
|
||||||
|
//: #### 函数类型
|
||||||
|
/*:
|
||||||
|
每一个函数都是有类型的,函数类型由行式参数类型、返回值类型组成的
|
||||||
|
*/
|
||||||
|
// 函数类型 () -> Void 或者 () -> ()
|
||||||
|
func test() {}
|
||||||
|
|
||||||
|
// 函数类型 (Int, Int) -> Int
|
||||||
|
func sum(a: Int, b: Int) -> Int {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义变量
|
||||||
|
var fn: (Int, Int) -> Int = sum
|
||||||
|
fn(2, 3) // 调用时不需要参数标签
|
||||||
|
|
||||||
|
// 函数类型作为函数参数
|
||||||
|
func sum(v1: Int, v2: Int) -> Int {
|
||||||
|
v1 + v2
|
||||||
|
}
|
||||||
|
|
||||||
|
func difference(v1: Int, v2: Int) -> Int {
|
||||||
|
v1 - v2
|
||||||
|
}
|
||||||
|
|
||||||
|
func printResult(_ mathFn: (Int, Int) -> Int, _ a: Int, _ b: Int) {
|
||||||
|
print("Result: \(mathFn(a, b))")
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(sum, 5, 2)
|
||||||
|
printResult(difference, 5, 2)
|
||||||
|
|
||||||
|
|
||||||
|
// 函数类型作为返回值,返回值是函数的函数叫做高级函数(Higher-Order Function)
|
||||||
|
func next(_ input: Int) -> Int {
|
||||||
|
input + 1
|
||||||
|
}
|
||||||
|
func previous(_ input: Int) -> Int {
|
||||||
|
input - 1
|
||||||
|
}
|
||||||
|
// 函数的返回值为 (Int) -> Int
|
||||||
|
func forward(_ forword: Bool) -> (Int) -> Int {
|
||||||
|
forword? next: previous
|
||||||
|
}
|
||||||
|
|
||||||
|
//: #### typealias 类型别名
|
||||||
|
typealias Byte = Int8
|
||||||
|
typealias Short = Int16
|
||||||
|
typealias Long = Int64
|
||||||
|
|
||||||
|
typealias Date = (year: Int, month: Int, day: Int)
|
||||||
|
func test (_ date: Date) {
|
||||||
|
print(date.0)
|
||||||
|
print(date.year)
|
||||||
|
}
|
||||||
|
|
||||||
|
test((2011,11,1))
|
||||||
|
|
||||||
|
typealias intFn = (Int, Int) -> Int
|
||||||
|
|
||||||
|
|
||||||
|
//: ### 嵌套函数(Nested Function)
|
||||||
|
/*:
|
||||||
|
嵌套函数:(Nested Function)
|
||||||
|
- 将函数定义在函数的内部
|
||||||
|
|
||||||
|
*/
|
||||||
|
// 将函数定义在函数的内部
|
||||||
|
func forward(_ forword: Bool) -> (Int) -> Int {
|
||||||
|
func next(_ input: Int) -> Int {
|
||||||
|
input + 1
|
||||||
|
}
|
||||||
|
func previous(_ input: Int) -> Int {
|
||||||
|
input - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
forword? next: previous
|
||||||
|
}
|
||||||
|
forward(true)(3)
|
||||||
|
forward(false)(3)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//: [Next](@next)
|
//: [Next](@next)
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user