1.函數(shù)的定義與調(diào)用
-
定義
func say(name:String) -> String{return "hello", + name + "!" }
-
調(diào)用
print(say("wanwan")) //hello,wanwan!
-
無參函數(shù)
func say() -> String{ return "hello,wanwan" } print(say()) //hello,wanwan
-
多參函數(shù)
func say(name:String,isgirl:Bool) -> String{
if isgirl{
return "hello", + name + "!"
}else{
return "hello"
}} * 調(diào)用 print(say("wanwan",ture)) //hello,wanwan!
-
無返回值函數(shù)
func say(name:String){ print("hello,wanwan") } say("wanwan") 因?yàn)檫@個(gè)函數(shù)不需要返回值茁计,所以這個(gè)函數(shù)的定義中沒有返回箭頭(->)和返回類型舅列。
-
當(dāng)一個(gè)函數(shù)調(diào)用另一個(gè)函數(shù)的時(shí)候溃论,該函數(shù)返回值可以忽略
func inputsome(name:String) -> Int { print("name\(name)") return name.characters.count } func lala(name: String) { inputsome(name) }
-
多重返回值
func minmax(array: [Int]) -> (min: Int, max: Int) { var currenmin = array[0] var currenmax = array[0] for value in array[1..<array.count]{ if value < currenmin{ currenmin = value }else if value > currenmax{ currenmax = value } } return (currenmin,currenmax) } minmax([1,2,3,4,5,6,7,8,8])
-
函數(shù)參數(shù)名稱
func come(first:String,last:String) { print("\(first)\(last)") } come("wanwan", last: "heihei")
函數(shù)有內(nèi)部參數(shù)名和外部參數(shù)名塘砸,外部參數(shù)省略第一個(gè)參數(shù)名,第二個(gè)及后面的都需要帶上參數(shù)名墓塌。
-
指定外部參數(shù)名
可以在局部參數(shù)名前指定外部參數(shù)名霞捡,中間空哥分開 func some(outname inname:String) { } some(outname: "wanwan") func sayHello(to person: String, and anotherPerson: String) -> String { return "Hello \(person) and \(anotherPerson)!" } print(sayHello(to: "Bill", and: "Ted")) // prints "Hello Bill and Ted!"
-
忽略外部參數(shù)名
func come(firat:Int , last : Int){ } come(1,2)
-
默認(rèn)參數(shù)
func come(firat:Int =12){ print(firat) } come() //12
-
可變參數(shù)
func some(number:Double...) -> Double { var total:Double = 0 for num in number { total += num } return total } some(1,2,3)
-
輸入輸出參數(shù)
函數(shù)參數(shù)默認(rèn)是常量,不能在函數(shù)內(nèi)部修改參數(shù)值愉昆,如果想修改睦裳,并且函數(shù)調(diào)用結(jié)束后這些修改仍然存在,就要定義為輸入輸出參數(shù):inout
func swapTwoInts(inout a: Int, inout _ b: Int) { let temporaryA = a a = b b = temporaryA } var someInt = 3 var anotherInt = 107 swapTwoInts(&someInt, &anotherInt) print("someInt is now \(someInt), and anotherInt is now \(anotherInt)") // prints "someInt is now 107, and anotherInt is now 3"
注意 參數(shù)名必須用
inout
標(biāo)記 調(diào)用必須用指針&
2.函數(shù)類型
每個(gè)函數(shù)都有種特定的函數(shù)類型撼唾,由函數(shù)的參數(shù)類型和返回類型組成廉邑。
func addTwoInts(a: Int, _ b: Int) -> Int {
return a + b
}
func multiplyTwoInts(a: Int, _ b: Int) -> Int {
return a * b
}
這兩個(gè)函數(shù)的類型是 (Int, Int) -> Int,可以解讀為“這個(gè)函數(shù)類型有兩個(gè) Int 型的參數(shù)并返回一個(gè) Int 型的值倒谷≈朊桑”
3.使用函數(shù)類型
-
定義一個(gè)
有兩個(gè)Int類型的參數(shù)并返回一個(gè)Int型的值
的函數(shù)變量,并將addTwoInts函數(shù)賦值給它
func addTwoInts(a: Int, _ b: Int) -> Int {
return a + b
}var mathadd: (Int, Int) -> Int = addTwoInts 現(xiàn)在可以用 `mathadd`調(diào)用被賦值的函數(shù) mathadd(1,4) //5
-
相同類型的不同函數(shù)可以賦值給同一變量
func multiplyTwoInts(a: Int, _ b: Int) -> Int { return a * b } mathadd = multiplyTwoInts marhadd(2,3) //6
4.函數(shù)類型作為參數(shù)類型
定義一個(gè)`aa`類型的函數(shù)渤愁,再定義一個(gè)包含`aa`類型函數(shù)的函數(shù)牵祟,調(diào)用的時(shí)候傳入`aa`類型的函數(shù)。
5.函數(shù)類型作為返回類型
先定義幾個(gè)函數(shù),這兩個(gè)函數(shù)類型都是 (Int) -> Int
func stepfor(input:Int) -> Int {
return input+1
}
func stepback(input:Int) -> Int {
return input-1
}
下面這個(gè)函數(shù)的返回類型為(Int) -> Int
func choose(back:Bool) -> (Int)->Int {
return back ? stepfor : stepback
}
調(diào)用:
var value = 3
let over = choose(value >0) //這時(shí)候的返回結(jié)果應(yīng)該是`stepfor`這個(gè)函數(shù)
over(4) // 給這個(gè)函數(shù)賦值
print(over(4)) //打印出來為5
上述步驟其實(shí)可以簡化
let over = choose(value>0)(4) //5
6.嵌套函數(shù)
全局函數(shù)可以被外界訪問抖格,嵌套函數(shù)只能被它的外圍函數(shù)調(diào)用诺苹,上面的例子可以改寫為嵌套函數(shù):
func choose(back:Bool) -> (Int)->Int {
func stepfor(input:Int) -> Int { return input+1}
func stepback(input:Int) -> Int {return input-1}
return back ? stepfor : stepback
}
調(diào)用:
var value = 3
let over = choose(value>0)(4) //5