不帶參數(shù)的函數(shù)
func helloWord() -> String { return "Hello word" }
帶一個參數(shù)的函數(shù)
func helloWord(_ hello:String) -> String { return hello } helloWord("hello world")
帶多個參數(shù)的函數(shù)
func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent); } sayHelloWorld("xiaoming", say: "hello world")
不帶返回值的函數(shù)
func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent) } sayHelloWorld("xiaoming", say: "hello world")
帶返回值得函數(shù)
func helloWorld(_ hello: String) ->String{ return hello } let hello = helloWorld("hello world")
帶多個返回值的函數(shù)
- 帶多個返回值的函數(shù)返回值以元組的形式返回
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("min is \(bounds.min) and max is \(bounds.max)") // 打印 "min is -6 and max is 109"
- 需要注意的是,元組的成員不需要在元組從函數(shù)中返回時命名,因為它們的名字已經(jīng)在函數(shù)返回類型中指定了。
參數(shù)
函數(shù)參數(shù)標簽和參數(shù)名稱
每個函數(shù)參數(shù)都有一個參數(shù)標簽( argument label )以及一個參數(shù)名稱( parameter name )。參數(shù)標簽在調(diào)用函數(shù)的時候使用;調(diào)用的時候需要將函數(shù)的參數(shù)標簽寫在對應(yīng)的參數(shù)前面。參數(shù)名稱在函數(shù)的實現(xiàn)中使用。默認情況下,函數(shù)參數(shù)使用參數(shù)名稱來作為它們的參數(shù)標簽。
參數(shù)標簽:
- 供函數(shù)外部使用,多個參數(shù)標簽可以相同,但是為了方便調(diào)用,盡量不要相同。
- 可以指定參數(shù)標簽 下例中perdonName 是指定的參數(shù)標簽 函數(shù)調(diào)用時需要顯示參數(shù)標簽
func sayHelloWorld(perdonName person:String, say sayMsg:String){ print(person + "say" + sayMsg) } sayHelloWorld(perdonName: "xiaoming", say: "hello world")
- 參數(shù)標簽可以隱藏 隱藏的參數(shù)標簽以 "_" 代替 函數(shù)調(diào)用時不用顯示
func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent) } sayHelloWorld("xiaoming", say: "hello world")
- 如果不指定參數(shù)標簽 默認參數(shù)名稱為參數(shù)標簽
- 如果指定了參數(shù)標簽 在函數(shù)調(diào)用時必須使用這個標簽
參數(shù)名稱:
- 供函數(shù)內(nèi)部使用,并且不能相同。
默認參數(shù)值
在函數(shù)體中可以為任意一個參數(shù)給定一個默認值,當定義默認值之后,在函數(shù)調(diào)用時可以忽略這個參數(shù)。
func xiaomingSayHelloWorld(_ person:String, sayMsg:String = "hello world"){ print(person + "say" + sayMsg) } xiaomingSayHelloWorld("xiaoming") xiaomingSayHelloWorld("xiaoming", sayMsg: "hello")
- 函數(shù)調(diào)用時如果忽略這個參數(shù),其會將默認值傳入函數(shù)內(nèi)。
- 函數(shù)調(diào)用時如果沒有忽略這個參數(shù),其將會忽略默認的值,傳入調(diào)用時傳入的值
將不帶有默認值的參數(shù)放在函數(shù)參數(shù)列表的最前。一般來說,沒有默認值的參數(shù)更加的重要,將不帶默認值的參數(shù)放在最前保證在函數(shù)調(diào)用時,非默認參數(shù)的順序是一致的,同時也使得相同的函數(shù)在不同情況下調(diào)用時顯得更為清晰。
可變參數(shù)
- 通過在變量類型名后面加入(...)的方式來定義可變參數(shù)
- 一個可變參數(shù)可以接受零個或多個參數(shù)
- 函數(shù)調(diào)用時,用可變參數(shù)來指定函數(shù)參數(shù)可以被傳入不確定數(shù)量的輸入值
func xiaomingSay(_ sayMsg:String...) -> String{ var sayMessage:String = "" for msg in sayMsg {sayMessage = sayMessage + "" + msg + " "} return sayMessage } let say = xiaomingSay("今天是1月17號","還有幾天就可以回家啦")
注意:
一個函數(shù)最多只能擁有一個可變參數(shù)。
輸入輸出參數(shù)
- 函數(shù)的參數(shù)默認是常量,如果想要修改函數(shù)參數(shù)的值,并且想要這些修改在函數(shù)調(diào)用之后仍然存在,那么需要把這個參數(shù)定義為輸入輸出函數(shù)。
- 定義一個輸入輸出參數(shù)時,在參數(shù)定義前加 inout 關(guān)鍵字。
- 一個輸入輸出參數(shù)有傳入函數(shù)的值,這個值被函數(shù)修改,然后被傳出函數(shù),替換原來的值。
- 只能傳遞變量給輸入輸出參數(shù)。不能傳入常量或者字面量,因為這些量是不能被修改的。當傳入的參數(shù)作為輸入輸出參數(shù)時,需要在參數(shù)名前加 & 符,表示這個值可以被函數(shù)修改。
注意:
輸入輸出參數(shù)不能有默認值,而且可變參數(shù)不能用 inout 標記。
var a1 = 10 var b1 = 20 func swapTwoInts(_ a: inout Int,_ b: inout Int){ let tempA = a a = b b = tempA } swapTwoInts(&a1, &b1)
注意:
輸入輸出參數(shù)和返回值是不一樣的。上面的 swapTwoInts 函數(shù)并沒有定義任何返回值,但仍然修改了 someInt 和 anotherInt 的值。輸入輸出參數(shù)是函數(shù)對函數(shù)體外產(chǎn)生影響的另一種方式。
函數(shù)類型
- 每個函數(shù)都有種特定的函數(shù)類型,函數(shù)的類型由函數(shù)的參數(shù)類型和返回類型組成。
func hello(hello: String) ->String{ return hello }
hello函數(shù)的函數(shù)類型為 (String) -> String
func helloWorld(){ print("hello world") }
helloWorld 的函數(shù)類型為 () -> void
函數(shù)類型做為函數(shù)參數(shù)
func hello(hello: String) ->String{ return hello } func helloTwo(_ say: (String) -> String, sayMsg: String){ print(say(sayMsg)) } helloTwo(hello, sayMsg: "hello")
函數(shù)類型做為函數(shù)返回值
func stepForward(_ input: Int) -> Int { return input + 1 } func stepBackward(_ input: Int) -> Int { return input - 1 } func chooseStepFunction(backward: Bool) -> (Int) -> Int { return backward ? stepBackward : stepForward } var currentValue = 3 let moveNearerToZero = chooseStepFunction(backward: currentValue > 0) // moveNearerToZero 現(xiàn)在指向 stepBackward() 函數(shù)。
嵌套函數(shù)
- 把函數(shù)定義到別的函數(shù)體中,叫做嵌套函數(shù)
func chooseStepFunction(backward: Bool) -> (Int) -> Int { func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } return backward ? stepBackward : stepForward } var currentValue = -4 let moveNearerToZero = chooseStepFunction(backward: currentValue > 0) // moveNearerToZero now refers to the nested stepForward() function while currentValue != 0 { print("\(currentValue)... ") currentValue = moveNearerToZero(currentValue) } print("zero!") // -4... // -3... // -2... // -1... // zero!