1.多重輸入參數:函數可以有多個輸入參數,寫在圓括號中,用逗號分隔。
func halfOpenRangeLength(start: Int, end: Int) -> Int {
return end - start
}
2.無參函數:函數可以沒有參數
func sayHelloWorld() -> String {
return "hello, world"
}
3.多參量函數:函數可以有多種輸入參數,這寫參數被包含在函數的括號之中,以逗號分隔.
func sayHello(personName: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return sayHelloAgain(personName)
} else {
return sayHello(personName)
}
}
4.無返回值函數:沒有定義返回類型的函數會返回特殊的值,叫 Void
func sayGoodbye(personName: String) {
print("Goodbye, \(personName)!")
}
5.多重返回值函數:可以用元組(tuple)類型讓多個值作爲一個複合值從函數中返回。
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)
}
6.指定外部參數名:你可以在本地參數名前指定外部參數名,中間以空格分隔.
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted"))
7.忽略外部參數名:不想爲第二個及後續的參數設置參數名,用一個下劃線(_)代替一個明確地參數名.
func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
// function body goes here
// firstParameterName and secondParameterName refer to
// the argument values for the first and second parameters
}
someFunction(1, 2)
8. 默認參數值:在函數體中爲每個參數定義默認值(Deafult Values)。當默認值被定義後,調用這個函數時可以忽略這個參數。
func someFunction(parameterWithDefault: Int = 12) {
// function body goes here
// if no arguments are passed to the function call,
// value of parameterWithDefault is 42
}
someFunction(6) // parameterWithDefault is 6
someFunction() // parameterWithDefault is 12
9.可變參數:一個可變參數(variadic parameter)可以接受零個或多個值。函數調用時,你可以用可變參數來傳入不確定數量的輸入參數。通過在變量類型名後面加入(...)的方式來定義可變參數。
傳入可變參數的值在函數體內當做這個類型的一個數組。例如,一個叫做 numbers 的 Double... 型可變參數,在函數體內可以當做一個叫 numbers 的 Double[] 型的數組常量。
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)
10.常量參數和變量參數:函數參數默認是常量。試圖在函數體中更改參數值將會導致編譯錯誤。變量參數不是常量,你可以在函數中把它當做新的可修改副本來使用.
func alignRight(var string: String, totalLength: Int, pad: Character) -> String {
let amountToPad = totalLength - string.characters.count
if amountToPad < 1 {
return string
}
let padString = String(pad)
for _ in 1...amountToPad {
string = padString + string
}
return string
}
let originalString = "hello"
let paddedString = alignRight(originalString, totalLength: 10, pad: "-")
11.輸入輸出參數:一個函數可以修改參數的值,並且想要在這些修改在函數調用結束後仍然存在,那麼就應該把這個參數定義爲輸入輸出參數.定義一個輸入輸出參數時,在參數定義前加 inout 關鍵字。一個輸入輸出參數有傳入函數的值,這個值被函數修改,然後被傳出函數,替換原來的值。
你只能將變量作爲輸入輸出參數。你不能傳入常量或者字面量(literal value),因爲這些量是不能被修改的。當傳入的參數作爲輸入輸出參數時,需要在參數前加&符,表示這個值可以被函數修改。
func swapTwoInts(inout a: Int, inout _ b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
12. 嵌套函數:嵌套函數是對外界不可見的,但是可以被他們封閉函數(enclosing function)來調用。一個封閉函數也可以返回它的某一個嵌套函數,使得這個函數可以在其他域中被使用。
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)