swift學(xué)習(xí) --- 函數(shù)

不帶參數(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ù)標(biāo)簽和參數(shù)名稱

每個函數(shù)參數(shù)都有一個參數(shù)標(biāo)簽( argument label )以及一個參數(shù)名稱( parameter name )。參數(shù)標(biāo)簽在調(diào)用函數(shù)的時候使用;調(diào)用的時候需要將函數(shù)的參數(shù)標(biāo)簽寫在對應(yīng)的參數(shù)前面。參數(shù)名稱在函數(shù)的實現(xiàn)中使用。默認(rèn)情況下,函數(shù)參數(shù)使用參數(shù)名稱來作為它們的參數(shù)標(biāo)簽。

參數(shù)標(biāo)簽:
  1. 供函數(shù)外部使用,多個參數(shù)標(biāo)簽可以相同,但是為了方便調(diào)用,盡量不要相同。
  2. 可以指定參數(shù)標(biāo)簽 下例中perdonName 是指定的參數(shù)標(biāo)簽 函數(shù)調(diào)用時需要顯示參數(shù)標(biāo)簽
    func sayHelloWorld(perdonName person:String, say sayMsg:String){ print(person + "say" + sayMsg) } sayHelloWorld(perdonName: "xiaoming", say: "hello world")
  3. 參數(shù)標(biāo)簽可以隱藏 隱藏的參數(shù)標(biāo)簽以 "_" 代替 函數(shù)調(diào)用時不用顯示
    func sayHelloWorld(_ person:String, say sayContent:String){ print(person + "say" + sayContent) } sayHelloWorld("xiaoming", say: "hello world")
  4. 如果不指定參數(shù)標(biāo)簽 默認(rèn)參數(shù)名稱為參數(shù)標(biāo)簽
  5. 如果指定了參數(shù)標(biāo)簽 在函數(shù)調(diào)用時必須使用這個標(biāo)簽
參數(shù)名稱:
  1. 供函數(shù)內(nèi)部使用,并且不能相同。
默認(rèn)參數(shù)值

在函數(shù)體中可以為任意一個參數(shù)給定一個默認(rèn)值,當(dāng)定義默認(rèn)值之后,在函數(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ù),其會將默認(rèn)值傳入函數(shù)內(nèi)。
  • 函數(shù)調(diào)用時如果沒有忽略這個參數(shù),其將會忽略默認(rèn)的值,傳入調(diào)用時傳入的值

將不帶有默認(rèn)值的參數(shù)放在函數(shù)參數(shù)列表的最前。一般來說,沒有默認(rèn)值的參數(shù)更加的重要,將不帶默認(rèn)值的參數(shù)放在最前保證在函數(shù)調(diào)用時,非默認(rèn)參數(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ù)默認(rèn)是常量,如果想要修改函數(shù)參數(shù)的值,并且想要這些修改在函數(shù)調(diào)用之后仍然存在,那么需要把這個參數(shù)定義為輸入輸出函數(shù)
  • 定義一個輸入輸出參數(shù)時,在參數(shù)定義前加 inout 關(guān)鍵字。
  • 一個輸入輸出參數(shù)有傳入函數(shù)的值,這個值被函數(shù)修改,然后被傳出函數(shù),替換原來的值。
  • 只能傳遞變量給輸入輸出參數(shù)。不能傳入常量或者字面量,因為這些量是不能被修改的。當(dāng)傳入的參數(shù)作為輸入輸出參數(shù)時,需要在參數(shù)名前加 & 符,表示這個值可以被函數(shù)修改。

注意:
輸入輸出參數(shù)不能有默認(rèn)值,而且可變參數(shù)不能用 inout 標(biāo)記。

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!

參考文獻(xiàn)

The Swift Programming Language 中文版 wiki - 函數(shù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 86.復(fù)合 Cases 共享相同代碼塊的多個switch 分支 分支可以合并, 寫在分支后用逗號分開。如果任何模式...
    無灃閱讀 1,438評論 1 5
  • 定義與調(diào)用 用func作為前綴來標(biāo)志這是一個函數(shù),用 (->) 加返回類型的名字表示該函數(shù)返回什么類型 參數(shù)與返回...
    JaneJie閱讀 357評論 0 1
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile麗語閱讀 3,864評論 0 6
  • 2014年的蘋果全球開發(fā)者大會(WWDC),當(dāng)Craig Federighi向全世界宣布“We have new ...
    yeshenlong520閱讀 2,311評論 0 9
  • 文/秀逗 11月10日 大風(fēng)天氣 明日1111 今日出門 處處是狗糧 甜膩膩 紅果果 曾經(jīng)聽聞 將喜悅綁縛于自己...
    秀逗茉莉閱讀 146評論 5 8