添加swift标签:06_Functions.md

This commit is contained in:
iceskysl
2014-06-13 12:17:37 +08:00
parent 26f73f5b19
commit 196acc804e

View File

@ -27,19 +27,23 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
在下面例子中的函数叫做`"greetingForPerson"`,之所以叫这个名字是因为这个函数用一个人的名字当做输入,并返回给这个人的问候语。为了完成这个任务,你定义一个输入参数-一个叫做 `personName``String` 值,和一个包含给这个人问候语的 `String` 类型的返回值: 在下面例子中的函数叫做`"greetingForPerson"`,之所以叫这个名字是因为这个函数用一个人的名字当做输入,并返回给这个人的问候语。为了完成这个任务,你定义一个输入参数-一个叫做 `personName``String` 值,和一个包含给这个人问候语的 `String` 类型的返回值:
func sayHello(personName: String) -> String { ```swift
func sayHello(personName: String) -> String {
let greeting = "Hello, " + personName + "!" let greeting = "Hello, " + personName + "!"
return greeting return greeting
} }
```
所有的这些信息汇总起来成为函数的定义,并以 `func` 作为前缀。指定函数返回类型时,用返回箭头 `->`(一个连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。 所有的这些信息汇总起来成为函数的定义,并以 `func` 作为前缀。指定函数返回类型时,用返回箭头 `->`(一个连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。
该定义描述了函数做什么,它期望接收什么和执行结束时它返回的结果是什么。这样的定义使的函数可以在别的地方以一种清晰的方式被调用: 该定义描述了函数做什么,它期望接收什么和执行结束时它返回的结果是什么。这样的定义使的函数可以在别的地方以一种清晰的方式被调用:
println(sayHello("Anna")) ```swift
// prints "Hello, Anna!" println(sayHello("Anna"))
println(sayHello("Brian")) // prints "Hello, Anna!"
// prints "Hello, Brian! println(sayHello("Brian"))
// prints "Hello, Brian!
```
调用 `sayHello` 函数时,在圆括号中传给它一个 `String` 类型的实参。因为这个函数返回一个 `String` 类型的值,`sayHello` 可以被包含在 `println` 的调用中,用来输出这个函数的返回值,正如上面所示。 调用 `sayHello` 函数时,在圆括号中传给它一个 `String` 类型的实参。因为这个函数返回一个 `String` 类型的值,`sayHello` 可以被包含在 `println` 的调用中,用来输出这个函数的返回值,正如上面所示。
@ -49,11 +53,14 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
为了简化这个函数的定义,可以将问候消息的创建和返回写成一句: 为了简化这个函数的定义,可以将问候消息的创建和返回写成一句:
func sayHelloAgain(personName: String) -> String { ```swift
func sayHelloAgain(personName: String) -> String {
return "Hello again, " + personName + "!" return "Hello again, " + personName + "!"
} }
println(sayHelloAgain("Anna")) println(sayHelloAgain("Anna"))
// prints "Hello again, Anna! // prints "Hello again, Anna!
```
<a name="Function_Parameters_and_Return_Values"></a> <a name="Function_Parameters_and_Return_Values"></a>
## 函数参数与返回值Function Parameters and Return Values ## 函数参数与返回值Function Parameters and Return Values
@ -65,21 +72,24 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面这个函数用一个半开区间的开始点和结束点,计算出这个范围内包含多少数字: 下面这个函数用一个半开区间的开始点和结束点,计算出这个范围内包含多少数字:
func halfOpenRangeLength(start: Int, end: Int) -> Int { ```swift
func halfOpenRangeLength(start: Int, end: Int) -> Int {
return end - start return end - start
} }
println(halfOpenRangeLength(1, 10)) println(halfOpenRangeLength(1, 10))
// prints "9 // prints "9
```
### 无参函数Functions Without Parameters ### 无参函数Functions Without Parameters
函数可以没有参数。下面这个函数就是一个无参函数,当被调用时,它返回固定的 `String` 消息: 函数可以没有参数。下面这个函数就是一个无参函数,当被调用时,它返回固定的 `String` 消息:
func sayHelloWorld() -> String { ```swift
func sayHelloWorld() -> String {
return "hello, world" return "hello, world"
} }
println(sayHelloWorld()) println(sayHelloWorld())
// prints "hello, world // prints "hello, world
```
尽管这个函数没有参数,但是定义中在函数名后还是需要一对圆括号。当被调用时,也需要在函数名后写一对圆括号。 尽管这个函数没有参数,但是定义中在函数名后还是需要一对圆括号。当被调用时,也需要在函数名后写一对圆括号。
@ -87,11 +97,13 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
函数可以没有返回值。下面是 `sayHello` 函数的另一个版本,叫 `waveGoodbye`,这个函数直接输出 `String` 值,而不是返回它: 函数可以没有返回值。下面是 `sayHello` 函数的另一个版本,叫 `waveGoodbye`,这个函数直接输出 `String` 值,而不是返回它:
func sayGoodbye(personName: String) { ```swift
func sayGoodbye(personName: String) {
println("Goodbye, \(personName)!") println("Goodbye, \(personName)!")
} }
sayGoodbye("Dave") sayGoodbye("Dave")
// prints "Goodbye, Dave! // prints "Goodbye, Dave!
```
因为这个函数不需要返回值,所以这个函数的定义中没有返回箭头(->)和返回类型。 因为这个函数不需要返回值,所以这个函数的定义中没有返回箭头(->)和返回类型。
@ -101,17 +113,20 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
被调用时,一个函数的返回值可以被忽略: 被调用时,一个函数的返回值可以被忽略:
func printAndCount(stringToPrint: String) -> Int { ```swift
func printAndCount(stringToPrint: String) -> Int {
println(stringToPrint) println(stringToPrint)
return countElements(stringToPrint) return countElements(stringToPrint)
} }
func printWithoutCounting(stringToPrint: String) { func printWithoutCounting(stringToPrint: String) {
printAndCount(stringToPrint) printAndCount(stringToPrint)
} }
printAndCount("hello, world") printAndCount("hello, world")
// prints "hello, world" and returns a value of 12 // prints "hello, world" and returns a value of 12
printWithoutCounting("hello, world") printWithoutCounting("hello, world")
// prints "hello, world" but does not return a value // prints "hello, world" but does not return a value
```
第一个函数 `printAndCount`,输出一个字符串并返回 `Int` 类型的字符数。第二个函数 `printWithoutCounting`调用了第一个函数,但是忽略了它的返回值。当第二个函数被调用时,消息依然会由第一个函数输出,但是返回值不会被用到。 第一个函数 `printAndCount`,输出一个字符串并返回 `Int` 类型的字符数。第二个函数 `printWithoutCounting`调用了第一个函数,但是忽略了它的返回值。当第二个函数被调用时,消息依然会由第一个函数输出,但是返回值不会被用到。
@ -125,7 +140,8 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面的这个例子中,`count` 函数用来计算一个字符串中元音,辅音和其他字母的个数(基于美式英语的标准)。 下面的这个例子中,`count` 函数用来计算一个字符串中元音,辅音和其他字母的个数(基于美式英语的标准)。
func count(string: String) -> (vowels: Int, consonants: Int, others: Int) { ```swift
func count(string: String) -> (vowels: Int, consonants: Int, others: Int) {
var vowels = 0, consonants = 0, others = 0 var vowels = 0, consonants = 0, others = 0
for character in string { for character in string {
switch String(character).lowercaseString { switch String(character).lowercaseString {
@ -139,7 +155,8 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
} }
} }
return (vowels, consonants, others) return (vowels, consonants, others)
} }
```
你可以用 `count` 函数来处理任何一个字符串,返回的值将是一个包含三个 `Int` 型值的元组tuple 你可以用 `count` 函数来处理任何一个字符串,返回的值将是一个包含三个 `Int` 型值的元组tuple
@ -153,10 +170,12 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
以上所有的函数都给它们的参数定义了`参数名parameter name` 以上所有的函数都给它们的参数定义了`参数名parameter name`
func someFunction(parameterName: Int) { ```swift
func someFunction(parameterName: Int) {
// function body goes here, and can use parameterName // function body goes here, and can use parameterName
// to refer to the argument value for that parameter // to refer to the argument value for that parameter
} }
```
但是,这些参数名仅在函数体中使用,不能在函数调用时使用。这种类型的参数名被称作`局部参数名local parameter name`,因为它们只能在函数体中使用。 但是,这些参数名仅在函数体中使用,不能在函数调用时使用。这种类型的参数名被称作`局部参数名local parameter name`,因为它们只能在函数体中使用。
@ -166,10 +185,12 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
如果你希望函数的使用者在调用函数时提供参数名字,那就需要给每个参数除了局部参数名外再定义一个`外部参数名`。外部参数名写在局部参数名之前,用空格分隔。 如果你希望函数的使用者在调用函数时提供参数名字,那就需要给每个参数除了局部参数名外再定义一个`外部参数名`。外部参数名写在局部参数名之前,用空格分隔。
func someFunction(externalParameterName localParameterName: Int) { ```swift
func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName // function body goes here, and can use localParameterName
// to refer to the argument value for that parameter // to refer to the argument value for that parameter
} }
```
> 注意: > 注意:
> >
@ -177,27 +198,35 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
以下是个例子,这个函数使用一个`结合者joiner`把两个字符串联在一起: 以下是个例子,这个函数使用一个`结合者joiner`把两个字符串联在一起:
func join(s1: String, s2: String, joiner: String) -> String { ```swift
func join(s1: String, s2: String, joiner: String) -> String {
return s1 + joiner + s2 return s1 + joiner + s2
} }
```
当你调用这个函数时,这三个字符串的用途是不清楚的: 当你调用这个函数时,这三个字符串的用途是不清楚的:
join("hello", "world", ", ") ```swift
// returns "hello, world join("hello", "world", ", ")
// returns "hello, world
```
为了让这些字符串的用途更为明显,我们为 `join` 函数添加外部参数名: 为了让这些字符串的用途更为明显,我们为 `join` 函数添加外部参数名:
func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { ```swift
func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String {
return s1 + joiner + s2 return s1 + joiner + s2
} }
```
在这个版本的 `join` 函数中,第一个参数有一个叫 `string` 的外部参数名和 `s1` 的局部参数名,第二个参数有一个叫 `toString` 的外部参数名和 `s2` 的局部参数名,第三个参数有一个叫 `withJoiner` 的外部参数名和 `joiner` 的局部参数名。 在这个版本的 `join` 函数中,第一个参数有一个叫 `string` 的外部参数名和 `s1` 的局部参数名,第二个参数有一个叫 `toString` 的外部参数名和 `s2` 的局部参数名,第三个参数有一个叫 `withJoiner` 的外部参数名和 `joiner` 的局部参数名。
现在,你可以使用这些外部参数名以一种清晰地方式来调用函数了: 现在,你可以使用这些外部参数名以一种清晰地方式来调用函数了:
join(string: "hello", toString: "world", withJoiner: ", ") ```swift
// returns "hello, world join(string: "hello", toString: "world", withJoiner: ", ")
// returns "hello, world
```
使用外部参数名让第二个版本的 `join` 函数的调用更为有表现力,更为通顺,同时还保持了函数体是可读的和有明确意图的。 使用外部参数名让第二个版本的 `join` 函数的调用更为有表现力,更为通顺,同时还保持了函数体是可读的和有明确意图的。
@ -211,20 +240,23 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面这个例子定义了一个叫 `containsCharacter` 的函数,使用`井号(#`的方式定义了外部参数名: 下面这个例子定义了一个叫 `containsCharacter` 的函数,使用`井号(#`的方式定义了外部参数名:
func containsCharacter(#string: String, #characterToFind: Character) -> Bool { ```swift
func containsCharacter(#string: String, #characterToFind: Character) -> Bool {
for character in string { for character in string {
if character == characterToFind { if character == characterToFind {
return true return true
} }
} }
return false return false
} }
```
这样定义参数名,使得函数体更为可读,清晰,同时也可以以一个不含糊的方式被调用: 这样定义参数名,使得函数体更为可读,清晰,同时也可以以一个不含糊的方式被调用:
let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v") ```swift
// containsAVee equals true, because "aardvark" contains a "v” let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")
// containsAVee equals true, because "aardvark" contains a "v”
```
### 默认参数值Default Parameter Values ### 默认参数值Default Parameter Values
@ -236,20 +268,25 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
以下是另一个版本的`join`函数,其中`joiner`有了默认参数值: 以下是另一个版本的`join`函数,其中`joiner`有了默认参数值:
func join(string s1: String, toString s2: String, withJoiner joiner: String = " ") -> String { ```swift
func join(string s1: String, toString s2: String, withJoiner joiner: String = " ") -> String {
return s1 + joiner + s2 return s1 + joiner + s2
} }
```
像第一个版本的 `join` 函数一样,如果 `joiner` 被赋值时,函数将使用这个字符串值来连接两个字符串: 像第一个版本的 `join` 函数一样,如果 `joiner` 被赋值时,函数将使用这个字符串值来连接两个字符串:
join(string: "hello", toString: "world", withJoiner: "-") ```swift
// returns "hello-world join(string: "hello", toString: "world", withJoiner: "-")
// returns "hello-world
```
当这个函数被调用时,如果 `joiner` 的值没有被指定,函数会使用默认值(" " 当这个函数被调用时,如果 `joiner` 的值没有被指定,函数会使用默认值(" "
join(string: "hello", toString:"world") ```swift
// returns "hello world" join(string: "hello", toString:"world")
// returns "hello world"
```
### 默认值参数的外部参数名External Names for Parameters with Default Values ### 默认值参数的外部参数名External Names for Parameters with Default Values
在大多数情况下,给带默认值的参数起一个外部参数名是很有用的。这样可以保证当函数被调用且带默认值的参数被提供值时,实参的意图是明显的。 在大多数情况下,给带默认值的参数起一个外部参数名是很有用的。这样可以保证当函数被调用且带默认值的参数被提供值时,实参的意图是明显的。
@ -258,14 +295,18 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面是 `join` 函数的另一个版本,这个版本中并没有为它的参数提供外部参数名,但是 `joiner` 参数依然有外部参数名: 下面是 `join` 函数的另一个版本,这个版本中并没有为它的参数提供外部参数名,但是 `joiner` 参数依然有外部参数名:
func join(s1: String, s2: String, joiner: String = " ") -> String { ```swift
func join(s1: String, s2: String, joiner: String = " ") -> String {
return s1 + joiner + s2 return s1 + joiner + s2
} }
```
在这个例子中Swift 自动为 `joiner` 提供了外部参数名。因此,当函数调用时,外部参数名必须使用,这样使得参数的用途变得清晰。 在这个例子中Swift 自动为 `joiner` 提供了外部参数名。因此,当函数调用时,外部参数名必须使用,这样使得参数的用途变得清晰。
join("hello", "world", joiner: "-") ```swift
// returns "hello-world" join("hello", "world", joiner: "-")
// returns "hello-world"
```
> 注意: > 注意:
> >
@ -279,17 +320,19 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面的这个函数用来计算一组任意长度数字的算术平均数: 下面的这个函数用来计算一组任意长度数字的算术平均数:
func arithmeticMean(numbers: Double...) -> Double { ```swift
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0 var total: Double = 0
for number in numbers { for number in numbers {
total += number total += number
} }
return total / Double(numbers.count) return total / Double(numbers.count)
} }
arithmeticMean(1, 2, 3, 4, 5) arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers // returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8, 19) arithmeticMean(3, 8, 19)
// returns 10.0, which is the arithmetic mean of these three numbers // returns 10.0, which is the arithmetic mean of these three numbers
```
> 注意: > 注意:
> >
@ -305,17 +348,19 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
通过在参数名前加关键字 `var` 来定义变量参数: 通过在参数名前加关键字 `var` 来定义变量参数:
func alignRight(var string: String, count: Int, pad: Character) -> String { ```swift
func alignRight(var string: String, count: Int, pad: Character) -> String {
let amountToPad = count - countElements(string) let amountToPad = count - countElements(string)
for _ in 1...amountToPad { for _ in 1...amountToPad {
string = pad + string string = pad + string
} }
return string return string
} }
let originalString = "hello" let originalString = "hello"
let paddedString = alignRight(originalString, 10, "-") let paddedString = alignRight(originalString, 10, "-")
// paddedString is equal to "-----hello" // paddedString is equal to "-----hello"
// originalString is still equal to "hello" // originalString is still equal to "hello"
```
这个例子中定义了一个新的叫做 `alignRight` 的函数,用来右对齐输入的字符串到一个长的输出字符串中。左侧空余的地方用指定的填充字符填充。这个例子中,字符串`"hello"`被转换成了`"-----hello"` 这个例子中定义了一个新的叫做 `alignRight` 的函数,用来右对齐输入的字符串到一个长的输出字符串中。左侧空余的地方用指定的填充字符填充。这个例子中,字符串`"hello"`被转换成了`"-----hello"`
@ -341,21 +386,25 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面是例子,`swapTwoInts` 函数,有两个分别叫做 `a``b` 的输出输出参数: 下面是例子,`swapTwoInts` 函数,有两个分别叫做 `a``b` 的输出输出参数:
func swapTwoInts(inout a: Int, inout b: Int) { ```swift
func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a let temporaryA = a
a = b a = b
b = temporaryA b = temporaryA
} }
```
这个 `swapTwoInts` 函数仅仅交换 `a``b` 的值。该函数先将 `a` 的值存到一个暂时常量 `temporaryA` 中,然后将 `b` 的值赋给 `a`,最后将 `temporaryA` 幅值给 `b` 这个 `swapTwoInts` 函数仅仅交换 `a``b` 的值。该函数先将 `a` 的值存到一个暂时常量 `temporaryA` 中,然后将 `b` 的值赋给 `a`,最后将 `temporaryA` 幅值给 `b`
你可以用两个 `Int` 型的变量来调用 `swapTwoInts`。需要注意的是,`someInt``anotherInt` 在传入 `swapTwoInts` 函数前,都加了 `&` 的前缀: 你可以用两个 `Int` 型的变量来调用 `swapTwoInts`。需要注意的是,`someInt``anotherInt` 在传入 `swapTwoInts` 函数前,都加了 `&` 的前缀:
var someInt = 3 ```swift
var anotherInt = 107 var someInt = 3
swapTwoInts(&someInt, &anotherInt) var anotherInt = 107
println("someInt is now \(someInt), and anotherInt is now \(anotherInt)") swapTwoInts(&someInt, &anotherInt)
// prints "someInt is now 107, and anotherInt is now 3” println("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// prints "someInt is now 107, and anotherInt is now 3”
```
从上面这个例子中,我们可以看到 `someInt``anotherInt` 的原始值在 `swapTwoInts` 函数中被修改,尽管它们的定义在函数体外。 从上面这个例子中,我们可以看到 `someInt``anotherInt` 的原始值在 `swapTwoInts` 函数中被修改,尽管它们的定义在函数体外。
@ -369,12 +418,14 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
例如: 例如:
func addTwoInts(a: Int, b: Int) -> Int { ```swift
func addTwoInts(a: Int, b: Int) -> Int {
return a + b return a + b
} }
func multiplyTwoInts(a: Int, b: Int) -> Int { func multiplyTwoInts(a: Int, b: Int) -> Int {
return a * b return a * b
} }
```
这个例子中定义了两个简单的数学函数:`addTwoInts``multiplyTwoInts`。这两个函数都传入两个 `Int` 类型, 返回一个合适的`Int`值。 这个例子中定义了两个简单的数学函数:`addTwoInts``multiplyTwoInts`。这两个函数都传入两个 `Int` 类型, 返回一个合适的`Int`值。
@ -382,9 +433,11 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面是另一个例子,一个没有参数,也没有返回值的函数: 下面是另一个例子,一个没有参数,也没有返回值的函数:
func printHelloWorld() { ```swift
func printHelloWorld() {
println("hello, world") println("hello, world")
} }
```
这个函数的类型是:`() -> ()`,或者叫“没有参数,并返回 `Void` 类型的函数。”。没有指定返回类型的函数总返回 `Void`。在Swift中`Void` 与空的元组是一样的。 这个函数的类型是:`() -> ()`,或者叫“没有参数,并返回 `Void` 类型的函数。”。没有指定返回类型的函数总返回 `Void`。在Swift中`Void` 与空的元组是一样的。
@ -392,7 +445,9 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
在Swift中使用函数类型就像使用其他类型一样。例如你可以定义一个类型为函数的常量或变量并将函数赋值给它 在Swift中使用函数类型就像使用其他类型一样。例如你可以定义一个类型为函数的常量或变量并将函数赋值给它
var mathFunction: (Int, Int) -> Int = addTwoInts ```swift
var mathFunction: (Int, Int) -> Int = addTwoInts
```
这个可以读作: 这个可以读作:
@ -402,31 +457,38 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
现在,你可以用 `mathFunction` 来调用被赋值的函数了: 现在,你可以用 `mathFunction` 来调用被赋值的函数了:
println("Result: \(mathFunction(2, 3))") ```swift
// prints "Result: 5 println("Result: \(mathFunction(2, 3))")
// prints "Result: 5
```
有相同匹配类型的不同函数可以被赋值给同一个变量,就像非函数类型的变量一样: 有相同匹配类型的不同函数可以被赋值给同一个变量,就像非函数类型的变量一样:
mathFunction = multiplyTwoInts ```swift
println("Result: \(mathFunction(2, 3))") mathFunction = multiplyTwoInts
// prints "Result: 6" println("Result: \(mathFunction(2, 3))")
// prints "Result: 6"
```
就像其他类型一样,当赋值一个函数给常量或变量时,你可以让 Swift 来推测其函数类型: 就像其他类型一样,当赋值一个函数给常量或变量时,你可以让 Swift 来推测其函数类型:
let anotherMathFunction = addTwoInts ```swift
// anotherMathFunction is inferred to be of type (Int, Int) -> Int let anotherMathFunction = addTwoInts
// anotherMathFunction is inferred to be of type (Int, Int) -> Int
```
### 函数类型作为参数类型Function Types as Parameter Types ### 函数类型作为参数类型Function Types as Parameter Types
你可以用`(Int, Int) -> Int`这样的函数类型作为另一个函数的参数类型。这样你可以将函数的一部分实现交由给函数的调用者。 你可以用`(Int, Int) -> Int`这样的函数类型作为另一个函数的参数类型。这样你可以将函数的一部分实现交由给函数的调用者。
下面是另一个例子,正如上面的函数一样,同样是输出某种数学运算结果: 下面是另一个例子,正如上面的函数一样,同样是输出某种数学运算结果:
func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { ```swift
func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
println("Result: \(mathFunction(a, b))") println("Result: \(mathFunction(a, b))")
} }
printMathResult(addTwoInts, 3, 5) printMathResult(addTwoInts, 3, 5)
// prints "Result: 8” // prints "Result: 8”
```
这个例子定义了 `printMathResult` 函数,它有三个参数:第一个参数叫 `mathFunction`,类型是`(Int, Int) -> Int`,你可以传入任何这种类型的函数;第二个和第三个参数叫 `a``b`,它们的类型都是 `Int`,这两个值作为已给的函数的输入值。 这个例子定义了 `printMathResult` 函数,它有三个参数:第一个参数叫 `mathFunction`,类型是`(Int, Int) -> Int`,你可以传入任何这种类型的函数;第二个和第三个参数叫 `a``b`,它们的类型都是 `Int`,这两个值作为已给的函数的输入值。
@ -440,40 +502,49 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
下面的这个例子中定义了两个简单函数,分别是 `stepForward``stepBackward``stepForward` 函数返回一个比输入值大一的值。`stepBackward` 函数返回一个比输入值小一的值。这两个函数的类型都是 `(Int) -> Int` 下面的这个例子中定义了两个简单函数,分别是 `stepForward``stepBackward``stepForward` 函数返回一个比输入值大一的值。`stepBackward` 函数返回一个比输入值小一的值。这两个函数的类型都是 `(Int) -> Int`
func stepForward(input: Int) -> Int { ```swift
func stepForward(input: Int) -> Int {
return input + 1 return input + 1
} }
func stepBackward(input: Int) -> Int { func stepBackward(input: Int) -> Int {
return input - 1 return input - 1
} }
```
下面这个叫做 `chooseStepFunction` 的函数,它的返回类型是 `(Int) -> Int` 的函数。`chooseStepFunction` 根据布尔值 `backwards` 来返回 `stepForward` 函数或 `stepBackward` 函数: 下面这个叫做 `chooseStepFunction` 的函数,它的返回类型是 `(Int) -> Int` 的函数。`chooseStepFunction` 根据布尔值 `backwards` 来返回 `stepForward` 函数或 `stepBackward` 函数:
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ```swift
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward return backwards ? stepBackward : stepForward
} }
```
你现在可以用 `chooseStepFunction` 来获得一个函数,不管是那个方向: 你现在可以用 `chooseStepFunction` 来获得一个函数,不管是那个方向:
var currentValue = 3 ```swift
let moveNearerToZero = chooseStepFunction(currentValue > 0) var currentValue = 3
// moveNearerToZero now refers to the stepBackward() function let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
```
上面这个例子中计算出从 `currentValue` 逐渐接近到`0`是需要向正数走还是向负数走。`currentValue` 的初始值是`3`,这意味着 `currentValue > 0` 是真的(`true`),这将使得 `chooseStepFunction` 返回 `stepBackward` 函数。一个指向返回的函数的引用保存在了 `moveNearerToZero` 常量中。 上面这个例子中计算出从 `currentValue` 逐渐接近到`0`是需要向正数走还是向负数走。`currentValue` 的初始值是`3`,这意味着 `currentValue > 0` 是真的(`true`),这将使得 `chooseStepFunction` 返回 `stepBackward` 函数。一个指向返回的函数的引用保存在了 `moveNearerToZero` 常量中。
现在,`moveNearerToZero` 指向了正确的函数,它可以被用来数到`0` 现在,`moveNearerToZero` 指向了正确的函数,它可以被用来数到`0`
println("Counting to zero:") ```swift
// Counting to zero: println("Counting to zero:")
while currentValue != 0 { // Counting to zero:
while currentValue != 0 {
println("\(currentValue)... ") println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue) currentValue = moveNearerToZero(currentValue)
} }
println("zero!") println("zero!")
// 3... // 3...
// 2... // 2...
// 1... // 1...
// zero! // zero!
```
<a name="Nested_Functions"></a> <a name="Nested_Functions"></a>
## 嵌套函数Nested Functions ## 嵌套函数Nested Functions
@ -483,21 +554,23 @@ Swift 统一的函数语法足够灵活,可以用来表示任何函数,包
你可以用返回嵌套函数的方式重写 `chooseStepFunction` 函数: 你可以用返回嵌套函数的方式重写 `chooseStepFunction` 函数:
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ```swift
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 } func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 } func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward return backwards ? stepBackward : stepForward
} }
var currentValue = -4 var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0) let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function // moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 { while currentValue != 0 {
println("\(currentValue)... ") println("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue) currentValue = moveNearerToZero(currentValue)
} }
println("zero!") println("zero!")
// -4... // -4...
// -3... // -3...
// -2... // -2...
// -1... // -1...
// zero! // zero!
```