有參數 有返回值
func greet(person:String) ->String{
? ? ?let greeting="Hello, "+person+"!"
? ? ?return greeting
}
有參數 無返回值
func greet(person:String) {
? ? ?print("Hello,\(person)!")
}
無參數 有返回值
func sayHelloWorld() ->String{
? ? return"hello, world"
}
無參數 無返回值
func noThing() {
?print("nothing")
}
有多個返回值 也就是返回一個元組
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)
}
Optional Tuple Return Types // 返回可為空的元組的函數
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)
}
Function Argument Labels and Parameter Names // 方法參數是帶標簽和參數名
func someFunction (argumentLabel parameterName:Int) {
? ? ? ? // In the function body, parameterName refers to the argument value
? ? ? ?// for that parameter.
}
func greet(person:String,from hometown:String) ->String{
? ? ? ?return"Hello\(person)!? Glad you could visit from\(hometown)."
}
print( greet(person:"Bill",from:"Cupertino") )
// Prints "Hello Bill!? Glad you could visit from Cupertino."
輸入時省略參數名
func someFunction( _ firstParameterName:Int,secondParameterName:Int) {
? ? ? // In the function body, firstParameterName and secondParameterName
? ? ?// refer to the argument values for the first and second parameters.
}
someFunction(1,secondParameterName:2)
默認的參數值 當省略那個參數,那么就會顯示默認值
func someFunction(parameterWithoutDefault:Int,parameterWithDefault:Int=12) {
? ? ? ? ?// If you omit the second argument when calling this function, then
? ? ? ? // the value of parameterWithDefault is 12 inside the function body.
}
someFunction(parameterWithoutDefault:3,parameterWithDefault:6)// parameterWithDefault is 6
someFunction(parameterWithoutDefault:4) // parameterWithDefault is 12
Variadic Parameters // 變量參數 ? ? ? ? ?可變參數接受指定類型的零個或多個值 ?(一個函數最多有一個可變參數)
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
In-Out Parameters? ? 可修改參數值的帶參函數? ? ? 默認情況下,函數參數是常量。 試圖從函數體內更改函數參數的值會導致編譯時錯誤。 這意味著您不能錯誤地更改參數的值。 如果希望函數修改參數的值,并且希望這些更改在函數調用結束后保留,請將該參數定義為in-out參數。
func swapTwoInts( _ a : inout ?Int, _ b : inout ?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"
Function Types ?// 函數類型
func addTwoInts( _ a : Int, _ b : Int) ->Int{
? ? ? ? ? ? ?return a+b
}
var mathFunction: (Int,Int) ?-> Int = addTwoInts // 聲明一個變量,變量類型是剛才定義的函數的類型
print("Result:\(mathFunction(2,3))") // 那么這個變量就等價于這樣一個函數 類似于C中的函數指針
// Prints "Result: 5"
mathFunction=multiplyTwoInts // 指向了另一個函數
print("Result:\(mathFunction(2,3))")
// Prints "Result: 6"
let anotherMathFunction=addTwoInts?// anotherMathFunction is inferred to be of type (Int, Int) -> Int
Function Types as Parameter Types // 函數作為參數 (內嵌函數)
func printMathResult( _ mathFunction: (Int,Int) ->Int,_a:Int,_b:Int) {
? ? ? ? ? ? ? print("Result:\(mathFunction(a,b))")
}
printMathResult(addTwoInts,3,5)?// Prints "Result: 8"
Function Types as Return Types // 函數作為返回值
func chooseStepFunction(backward:Bool) -> (Int) ->Int{ // 返回類型是 (Int) ->Int 是個函數
? ? ? return backward?stepBackward:stepForward
}
varcurrentValue=3
let moveNearerToZero = chooseStepFunction(backward:currentValue>0)
// moveNearerToZero now refers to the stepBackward() function
print("Counting to zero:")
// Counting to zero:
while currentValue != 0 {
? ? ? ?print("\(currentValue)... ")
? ? ? currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// 3...
// 2...
// 1...
// zero!
Nested Functions // 函數嵌套
func chooseStepFunction(backward:Bool) -> (Int) ->Int{
? ? ? ? ? ? ?func stepForward(input:Int) ?-> Int{returninput+1}
? ? ? ? ? ? ?func stepBackward(input:Int) ?-> Int{returninput-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!