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