一个朴实无华的目录
今日学习内容:
1.Swift 函数
1.1函数定义:使用关键字 func。
import Cocoa
func runoob(site: String) -> String {
return (site)
}
print(runoob(site: "www.runoob.com"))
1.2函数参数:以逗号分隔。
import Cocoa
func runoob(name: String, site: String) -> String {
return name + site
}
print(runoob(name: "菜鸟教程:", site: "www.runoob.com"))
print(runoob(name: "Google:", site: "www.google.com"))
1.3不带参数函数
import Cocoa
func sitename() -> String {
return "菜鸟教程"
}
print(sitename())
1.4元组作为函数返回值
用元组(tuple)类型让多个值作为一个复合值从函数中返回。
import Cocoa
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)
}
let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("最小值为 \(bounds.min) ,最大值为 \(bounds.max)")
将minMax(_:)函数改写为使用可选元组返回类型,并且当数组为空时返回nil:
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("最小值为 \(bounds.min),最大值为 \(bounds.max)")
}
1.5没有返回值函数
import Cocoa
func runoob(site: String) {
print("菜鸟教程官网:\(site)")
}
runoob(site: "http://www.runoob.com")
1.6函数参数名称
1.6.1局部参数名
import Cocoa
func sample(number: Int) {
print(number)
}
sample(number: 1)
sample(number: 2)
sample(number: 3)
1.6.2外部参数名
import Cocoa
func pow(firstArg a: Int, secondArg b: Int) -> Int {
var res = a
for _ in 1..<b {
res = res * a
}
print(res)
return res
}
pow(firstArg:5, secondArg:3)
1.7可变参数
import Cocoa
func vari<N>(members: N...){
for i in members {
print(i)
}
}
vari(members: 4,3,5)
vari(members: 4.5, 3.1, 5.6)
vari(members: "Google", "Baidu", "Runoob")
以上程序执行输出结果为:
4
3
5
4.5
3.1
5.6
Google
Baidu
Runoob
1.8常量,变量及 I/O 参数
1.如果想要声明一个变量参数,可以在参数定义前加 inout 关键字,这样就可以改变这个参数的值了。
2.一般默认的参数传递都是传值调用的,传入的参数在函数内改变,并不影响原来的那个参数。传入的只是这个参数的副本。
3.当传入的参数作为输入输出参数时,需要在参数名前加 & 符,表示这个值可以被函数修改。
import Cocoa
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var x = 1
var y = 5
swapTwoInts(&x, &y)
print("x 现在的值 \(x), y 现在的值 \(y)")
以上程序执行输出结果为:
x 现在的值 5, y 现在的值 1
1.8使用函数类型
import Cocoa
func sum(a: Int, b: Int) -> Int {
return a + b
}
var addition: (Int, Int) -> Int = sum
print("输出结果: \(addition(40, 89))")
以上程序执行输出结果为:
输出结果: 129
1.9函数类型作为参数类型、函数类型作为返回类型
func another(addition: (Int, Int) -> Int, a: Int, b: Int) {
print("输出结果: \(addition(a, b))")
}
another(addition: sum, a: 10, b: 20)
以上程序执行输出结果为:
输出结果: 129
输出结果: 30
1.10函数嵌套:外部的函数可以调用函数内定义的函数。
import Cocoa
func calcDecrement(forDecrement total: Int) -> () -> Int {
var overallDecrement = 0
func decrementer() -> Int {
overallDecrement -= total
return overallDecrement
}
return decrementer
}
let decrem = calcDecrement(forDecrement: 30)
print(decrem())
以上程序执行输出结果为:
-30