有参数 有返回值
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!