定義和使用函數(shù)
下面定義名稱 sayHello 的函數(shù),只有一個 String 類型的 name 參數(shù)挖炬,函數(shù)返回值為 String 類型揽浙。
func sayHello(name:String)->String{
? ? return "Hello,"+name
}
函數(shù)參數(shù)和返回值
沒有參數(shù)的函數(shù)
func helloWorld()->String{
? ?return"Hello World"
}
有多個參數(shù)的函數(shù),參數(shù)之間用逗號分割
func helloPeople(firtName:String,lastName:String)->String{?
? return"Hello, "+firtName+" "+lastName
}
沒有返回值的函數(shù)
func printName(firtName:String,lastName:String){
?print("Hello, "+firtName+" "+lastName)
}
有多個返回值的函數(shù),函數(shù)通過返回一個元組來返回多個值。
func tuplesFunction(name:String)->(hello:String,goodbye:String){
let hello="Hello, "+name
let goodbye="Goodbye, "+name
return(hello,goodbye)
}
函數(shù)參數(shù)名稱
函數(shù)的每一個參數(shù)都有外部名稱和內(nèi)部名稱意敛,外部名稱在調(diào)用函數(shù)時使用馅巷,內(nèi)部名稱在函數(shù)內(nèi)部實現(xiàn)中使用。
默認(rèn)情況,函數(shù)第一個參數(shù)會忽略外部名稱草姻,后面的參數(shù)外部名稱和內(nèi)部名稱一致钓猬,如下面的示例。
func sayHello(firtName:String,lastName:String){
print("Hello, "+firtName+" "+lastName)
}
sayHello("zhao",lastName:"Alex")
指定外部名稱
下面示例中 firstName 和 lastName 是外部名稱撩独,first 和 last 是內(nèi)部名稱敞曹,如果指定了外部名稱,調(diào)用函數(shù)時也要寫明外部名稱综膀。
func sayHello(firtName first:String,lastName last:String){
print("Hello, "+first+" "+last)
}
sayHello(firtName:"zhao",lastName:"Alex")
忽略外部名稱,用下劃線來忽略外部名稱澳迫。
func sayHello(firstName:String,_lastName:String){
print("Hello, "+firstName+" "+lastName)
}
sayHello("zhao","Alex")
默認(rèn)參數(shù)值
函數(shù)參數(shù)可以指定默認(rèn)值,在沒有傳入?yún)?shù)值時剧劝,此參數(shù)就使用默認(rèn)值橄登。
func someFunction(parameterWithDefault: Int = 12) {
// function body goes here
// if no arguments are passed to the function call,
// value of parameterWithDefault is 12
}
someFunction(6) // parameterWithDefault is 6
someFunction() // parameterWithDefault is 12
可變參數(shù)
函數(shù)接受零到多個參數(shù)值。
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
輸入輸出參數(shù)
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ù)類型
函數(shù)類型由參數(shù)類型和返回值類型構(gòu)成讥此,如下示例的函數(shù)類型就是 (String, String) -> String
func sayHello(firtName:String,lastName:String){
? ?print("Hello, "+firtName+" "+lastName)
}
使用函數(shù)類型
每個函數(shù)都有種特定的函數(shù)類型,由函數(shù)的參數(shù)類型和返回類型組成拢锹。
例如:
func addTwoInts(a: Int, _ b: Int) -> Int {
? ?return a + b
}
func multiplyTwoInts(a: Int, _ b: Int) -> Int {
? ?return a * b
}
這兩個函數(shù)的類型是(Int, Int) -> Int,可以解讀為“這個函數(shù)類型有兩個Int型的參數(shù)并返回一個Int型的值√言”卒稳。
下面是另一個例子,一個沒有參數(shù),也沒有返回值的函數(shù):
func printHelloWorld() {
print("hello, world")
}
使用函數(shù)類型
在 Swift 中,使用函數(shù)類型就像使用其他類型一樣。例如,你可以定義一個類型為函數(shù)的常量或變量,并將適當(dāng)
的函數(shù)賦值給它:
var mathFunction: (Int, Int) -> Int = addTwoInts
print("Result: \(mathFunction(2, 3))")
// prints "Result: 5"
函數(shù)類型作為參數(shù)類型
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
? ? print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// prints "Result: 8"
函數(shù)類型作為返回類型
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
? ?return input - 1
}
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
嵌套函數(shù)
func chooseStepFunction(backwards:Bool) ->Int{
? ?func stepForward(input:Int) ->Int{
? ? ? returninput +1
? ?}
? ?func stepBackward(input:Int) ->Int{
? ? ? ? returninput -1
? ? ?}
? returnbackwards ?stepBackward(4) :stepForward(3)
}
letcurrentValue =3
letmoveNearerToZero =chooseStepFunction(currentValue >0)
print(moveNearerToZero) ?// 3