函数的概念:
函数也叫做方法,是有名字的闭包。
函数的参数:
无参数
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld())```
#####有多个参数且未定义外部参数
func sayHello(personName: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return sayHelloAgain(personName)
} else {
return sayHello(personName)
}
}
print(sayHello("Tim", alreadyGreeted: true))
//多个参数调用时,如果没有声明外部参数第一个参数不用写外部参数。
// Prints "Hello again, Tim!" ```
有多个参数并且第一个参数定义了外部参数这时第一个参数的外部参数不能省略
func sayHello(personName personName: String,alreadyGreeted alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return sayHelloAgain(personName)
} else {
return sayHello(personName)
}
}
print(sayHello(personName: "Tim", alreadyGreeted: true))
//多个参数调用时,如果声明了外部参数第一个参数需要写外部参数。
// Prints "Hello again, Tim!"```
#####有多个参数且在参数名前面加“_”,调用时不需要写外部参数名
func sayHello(personName: String, _ alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return sayHelloAgain(personName)
} else {
return sayHello(personName)
}
}
print(sayHello("Tim", true))
//多个参数调用时,声明参数名前面加“_”,调用时不需要写外部参数名。
// Prints "Hello again, Tim!"```
带有默认参数调用的时候可以不用传参则使用默认参数
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```
#####可变参数,在参数类型后面加“...”代表参数个数可变但是每个函数最多只有一个可变参数
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```
函数通过传引用,通过在参数名前面加上inout关键字
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"```
####函数的返回值:
#####因为函数没有返回值所以这里不需要写返回值得箭头"->"
func sayGoodbye(personName: String) {
print("Goodbye, (personName)!")
}
sayGoodbye("Dave")
// Prints "Goodbye, Dave!"```
函数只有一个返回值
func printAndCount(stringToPrint: String) -> Int {
print(stringToPrint)
return stringToPrint.characters.count
}
let count = printAndCount("hello, world")
// prints "hello, world" and returns a value of 12```
#####函数还可以有多个返回值
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("min is (bounds.min) and max is (bounds.max)")
// 通过这样的方式接收多个返回值```
函数有可选返回值时在返回值括号外加“?”可以返回nil
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)
}```
####函数类型:
#####函数类型和其他类型一样使用这就意味着函数可以先声明再赋值并且让函数可以像参数一样传递,像参数一样返回
定义一个函数类型为(Int, Int) -> Int的函数他表示有两个Int型参数和一个Int型返回值的类型,addTwoInts对mathFunction这个函数赋值
func addTwoInts(a: Int, _ b: Int) -> Int {
return a + b
}
var mathFunction: (Int, Int) -> Int = addTwoInts
//函数类型做为参数类型使用定义一个名为mathFunction的参数的printMathResult函数并把上文的addTwoInts函数传入其中
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: (mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"```
函数类型做为返回值类型使用
//定义chooseStepFunction函数它的返回值是一个参数为Int返回值为Int的函数当Bool为true时返回stepBackward 当Bool为false时返回stepForward
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```
####函数里面定义函数:
Swift在函数中还可以声明和调用函数,如在chooseStepFunction函数中声明stepForward函数和stepBackward函数。并作为返回值返回
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
}```