国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

??? ?? ?? / Swift 函數(shù)

Swift 函數(shù)

Swift 函數(shù)用來完成特定任務的獨立的代碼塊。

Swift使用一個統(tǒng)一的語法來表示簡單的C語言風格的函數(shù)到復雜的Objective-C語言風格的方法。

  • 函數(shù)聲明: 告訴編譯器函數(shù)的名字,返回類型及參數(shù)。

  • 函數(shù)定義: 提供了函數(shù)的實體。

Swift 函數(shù)包含了參數(shù)類型及返回值類型:


函數(shù)定義

Swift 定義函數(shù)使用關鍵字 func。

定義函數(shù)的時候,可以指定一個或多個輸入?yún)?shù)和一個返回值類型。

每個函數(shù)都有一個函數(shù)名來描述它的功能。通過函數(shù)名以及對應類型的參數(shù)值來調(diào)用這個函數(shù)。函數(shù)的參數(shù)傳遞的順序必須與參數(shù)列表相同。

函數(shù)的實參傳遞的順序必須與形參列表相同,-> 后定義函數(shù)的返回值類型。

語法

func funcname(形參) -> returntype
{
   Statement1
   Statement2
   ……
   Statement N
   return parameters
}

實例

以下我們定義了一個函數(shù)名為 php 的函數(shù),形參的數(shù)據(jù)類型為 String,返回值也為 String:

import Cocoa

func php(site: String) -> String {
    return site
}
print(php("m.miracleart.cn"))

以上程序執(zhí)行輸出結果為:

m.miracleart.cn

函數(shù)調(diào)用

我們可以通過函數(shù)名以及對應類型的參數(shù)值來調(diào)用函數(shù),函數(shù)的參數(shù)傳遞的順序必須與參數(shù)列表相同。

以下我們定義了一個函數(shù)名為 php 的函數(shù),形參 site 的數(shù)據(jù)類型為 String,之后我們調(diào)用函數(shù)傳遞的實參也必須 String 類型,實參傳入函數(shù)體后,將直接返回,返回的數(shù)據(jù)類型為 String。

import Cocoa

func php(site: String) -> String {
    return site
}
print(php("m.miracleart.cn"))

以上程序執(zhí)行輸出結果為:

m.miracleart.cn

函數(shù)參數(shù)

函數(shù)可以接受一個或者多個參數(shù),我們也可以使用元組(tuple)向函數(shù)傳遞一個或多個參數(shù):

import Cocoa

func mult(no1: Int, no2: Int) -> Int {
   return no1*no2
}
print(mult(2, no2:20))
print(mult(3, no2:15))
print(mult(4, no2:30))

以上程序執(zhí)行輸出結果為:

40
45
120

不帶參數(shù)函數(shù)

我們可以創(chuàng)建不帶參數(shù)的函數(shù)。

語法:

func funcname() -> datatype {
   return datatype
}

實例

import Cocoa

func sitename() -> String {
    return "php中文網(wǎng)"
}
print(sitename())

以上程序執(zhí)行輸出結果為:

php中文網(wǎng)

元組作為函數(shù)返回值

函數(shù)返回值類型可以是字符串,整型,浮點型等。

元組與數(shù)組類似,不同的是,元組中的元素可以是任意類型,使用的是圓括號。

你可以用元組(tuple)類型讓多個值作為一個復合值從函數(shù)中返回。

下面的這個例子中,定義了一個名為minMax(_:)的函數(shù),作用是在一個Int數(shù)組中找出最小值與最大值。

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([8, -6, 2, 109, 3, 71])
print("最小值為 \(bounds.min) ,最大值為 \(bounds.max)")

minMax(_:)函數(shù)返回一個包含兩個Int值的元組,這些值被標記為min和max,以便查詢函數(shù)的返回值時可以通過名字訪問它們。

以上程序執(zhí)行輸出結果為:

最小值為 -6 ,最大值為 109

如果你不確定返回的元組一定不為nil,那么你可以返回一個可選的元組類型。

你可以通過在元組類型的右括號后放置一個問號來定義一個可選元組,例如(Int, Int)?或(String, Int, Bool)?

注意
可選元組類型如(Int, Int)?與元組包含可選類型如(Int?, Int?)是不同的.可選的元組類型,整個元組是可選的,而不只是元組中的每個元素值。

前面的minMax(_:)函數(shù)返回了一個包含兩個Int值的元組。但是函數(shù)不會對傳入的數(shù)組執(zhí)行任何安全檢查,如果array參數(shù)是一個空數(shù)組,如上定義的minMax(_:)在試圖訪問array[0]時會觸發(fā)一個運行時錯誤。

為了安全地處理這個"空數(shù)組"問題,將minMax(_:)函數(shù)改寫為使用可選元組返回類型,并且當數(shù)組為空時返回nil

import Cocoa

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([8, -6, 2, 109, 3, 71]) {
    print("最小值為 \(bounds.min),組大值為 \(bounds.max)")
}

以上程序執(zhí)行輸出結果為:

最小值為 -6,組大值為 109

沒有返回值函數(shù)

下面是 php(_:) 函數(shù)的另一個版本,這個函數(shù)接收php中文網(wǎng)官網(wǎng)網(wǎng)址參數(shù),沒有指定返回值類型,并直接輸出 String 值,而不是返回它:

import Cocoa

func php(site: String) {
    print("php中文網(wǎng)官網(wǎng):\(site)")
}
php("http://m.miracleart.cn")

以上程序執(zhí)行輸出結果為:

php中文網(wǎng)官網(wǎng):http://m.miracleart.cn

函數(shù)參數(shù)名稱

函數(shù)參數(shù)都有一個外部參數(shù)名和一個局部參數(shù)名。

局部參數(shù)名

局部參數(shù)名在函數(shù)的實現(xiàn)內(nèi)部使用。

func sample(number: Int) {
   println(number)
}

以上實例中 number 為局部參數(shù)名,只能在函數(shù)體內(nèi)使用。

import Cocoa

func sample(number: Int) {
   print(number)
}
sample(1)
sample(2)
sample(3)

以上程序執(zhí)行輸出結果為:

1
2
3

外部參數(shù)名

你可以在局部參數(shù)名前指定外部參數(shù)名,中間以空格分隔,外部參數(shù)名用于在函數(shù)調(diào)用時傳遞給函數(shù)的參數(shù)。

如下你可以定義以下兩個函數(shù)參數(shù)名并調(diào)用它:

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)

以上程序執(zhí)行輸出結果為:

125

注意
如果你提供了外部參數(shù)名,那么函數(shù)在被調(diào)用時,必須使用外部參數(shù)名。


可變參數(shù)

可變參數(shù)可以接受零個或多個值。函數(shù)調(diào)用時,你可以用可變參數(shù)來指定函數(shù)參數(shù),其數(shù)量是不確定的。

可變參數(shù)通過在變量類型名后面加入(...)的方式來定義。

import Cocoa

func vari<N>(members: N...){
    for i in members {
        print(i)
    }
}
vari(4,3,5)
vari(4.5, 3.1, 5.6)
vari("Google", "Baidu", "php")

以上程序執(zhí)行輸出結果為:

4
3
5
4.5
3.1
5.6
Google
Baidu
php

常量,變量及 I/O 參數(shù)

一般默認在函數(shù)中定義的參數(shù)都是常量參數(shù),也就是這個參數(shù)你只可以查詢使用,不能改變它的值。

如果想要聲明一個變量參數(shù),可以在前面加上var,這樣就可以改變這個參數(shù)的值了。

例如:

func  getName(var id:String).........

此時這個id值可以在函數(shù)中改變。

一般默認的參數(shù)傳遞都是傳值調(diào)用的,而不是傳引用。  所以傳入的參數(shù)在函數(shù)內(nèi)改變,并不影響原來的那個參數(shù)。傳入的只是這個參數(shù)的副本。

變量參數(shù),正如上面所述,僅僅能在函數(shù)體內(nèi)被更改。如果你想要一個函數(shù)可以修改參數(shù)的值,并且想要在這些修改在函數(shù)調(diào)用結束后仍然存在,那么就應該把這個參數(shù)定義為輸入輸出參數(shù)(In-Out Parameters)。

定義一個輸入輸出參數(shù)時,在參數(shù)定義前加 inout 關鍵字。一個輸入輸出參數(shù)有傳入函數(shù)的值,這個值被函數(shù)修改,然后被傳出函數(shù),替換原來的值。

實例

import Cocoa

func swapTwoInts(var a:Int,var b:Int){
    
    let t = a
    a = b
    b = t
}

var x = 0,y = 100
print("x = \(x) ;y = \(y)")

swapTwoInts(x, b:y)
print("x = \(x) ;y = \(y)")

以上程序執(zhí)行輸出結果為:

x = 0 ;y = 100
x = 0 ;y = 100
<p此時傳入的參數(shù)是原來值的副本,所以這個函數(shù)并不會交換兩個值。< p="">

修改方法是使用inout關鍵字:

import Cocoa

func swapTwoInts(inout a:Int,inout b:Int){
    
    let t = a
    a = b
    b = t
}

var x = 0,y = 100
print("x = \(x) ;y = \(y)")

swapTwoInts(&x, b:&y)
print("x = \(x) ;y = \(y)")

以上程序執(zhí)行輸出結果為:

x = 0 ;y = 100
x = 100 ;y = 0

函數(shù)類型及使用

每個函數(shù)都有種特定的函數(shù)類型,由函數(shù)的參數(shù)類型和返回類型組成。

func inputs(no1: Int, no2: Int) -> Int {
   return no1/no2
}

實例如下:

import Cocoa

func inputs(no1: Int, no2: Int) -> Int {
    return no1/no2
}
print(inputs(20,no2:10))
print(inputs(36,no2:6))

以上程序執(zhí)行輸出結果為:

2
6

以上函數(shù)定義了兩個 Int  參數(shù)類型,返回值也為 Int 類型。

接下來我們看下如下函數(shù),函數(shù)定義了參數(shù)為 String 類型,返回值為 String 類型。

Func inputstr(name: String) -> String {
   return name
}

函數(shù)也可以定義任何參數(shù)及類型,如下所示:

import Cocoa

func inputstr() {
   print("php中文網(wǎng)")
   print("m.miracleart.cn")
}
inputstr()

以上程序執(zhí)行輸出結果為:

php中文網(wǎng)
m.miracleart.cn

使用函數(shù)類型

在 Swift 中,使用函數(shù)類型就像使用其他類型一樣。例如,你可以定義一個類型為函數(shù)的常量或變量,并將適當?shù)暮瘮?shù)賦值給它:

var addition: (Int, Int) -> Int = sum

解析:

"定義一個叫做 addition 的變量,參數(shù)與返回值類型均是 Int ,并讓這個新變量指向 sum 函數(shù)"。

sumaddition 有同樣的類型,所以以上操作是合法的。

現(xiàn)在,你可以用 addition 來調(diào)用被賦值的函數(shù)了:

import Cocoa

func sum(a: Int, b: Int) -> Int {
   return a + b
}
var addition: (Int, Int) -> Int = sum
print("輸出結果: \(addition(40, 89))")

以上程序執(zhí)行輸出結果為:

輸出結果: 129

函數(shù)類型作為參數(shù)類型、函數(shù)類型作為返回類型

我們可以將函數(shù)作為參數(shù)傳遞給另外一個參數(shù):

import Cocoa

func sum(a: Int, b: Int) -> Int {
    return a + b
}
var addition: (Int, Int) -> Int = sum
print("輸出結果: \(addition(40, 89))")

func another(addition: (Int, Int) -> Int, a: Int, b: Int) {
    print("輸出結果: \(addition(a, b))")
}
another(sum, a: 10, b: 20)

以上程序執(zhí)行輸出結果為:

輸出結果: 129
輸出結果: 30

函數(shù)嵌套

函數(shù)嵌套指的是函數(shù)內(nèi)定義一個新的函數(shù),外部的函數(shù)可以調(diào)用函數(shù)內(nèi)定義的函數(shù)。

實例如下:

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())

以上程序執(zhí)行輸出結果為:

-30