发布于 2015-06-14 01:54:33 | 186 次阅读 | 评论: 0 | 来源: 网络整理

函数参数与返回值(Function Parameters and Return Values)

函数参数与返回值在Swift中极为灵活。你可以定义任何类型的函数,包括从只带一个未名参数的简单函数到复杂的带有表达性参数名和不同参数选项的复杂函数。

多重输入参数(Multiple Input Parameters)

函数可以有多个输入参数,写在圆括号中,用逗号分隔。

下面这个函数用一个半开区间的开始点和结束点,计算出这个范围内包含多少数字:


func halfOpenRangeLength(start: Int, end: Int) -> Int {
    return end - start
}
println(halfOpenRangeLength(1, 10))
// prints "9"

无参函数(Functions Without Parameters)

函数可以没有参数。下面这个函数就是一个无参函数,当被调用时,它返回固定的 String 消息:


func sayHelloWorld() -> String {
    return "hello, world"
}
println(sayHelloWorld())
// prints "hello, world"

尽管这个函数没有参数,但是定义中在函数名后还是需要一对圆括号。当被调用时,也需要在函数名后写一对圆括号。

无返回值函数(Functions Without Return Values)

函数可以没有返回值。下面是 sayHello 函数的另一个版本,叫 waveGoodbye,这个函数直接输出 String 值,而不是返回它:


func sayGoodbye(personName: String) {
    println("Goodbye, (personName)!")
}
sayGoodbye("Dave")
// prints "Goodbye, Dave!"

因为这个函数不需要返回值,所以这个函数的定义中没有返回箭头(->)和返回类型。


注意:
严格上来说,虽然没有返回值被定义,sayGoodbye 函数依然返回了值。没有定义返回类型的函数会返回特殊的值,叫 Void。它其实是一个空的元组(tuple),没有任何元素,可以写成()
 

被调用时,一个函数的返回值可以被忽略:


func printAndCount(stringToPrint: String) -> Int {
    println(stringToPrint)
    return countElements(stringToPrint)
}
func printWithoutCounting(stringToPrint: String) {
    printAndCount(stringToPrint)
}
printAndCount("hello, world")
// prints "hello, world" and returns a value of 12
printWithoutCounting("hello, world")
// prints "hello, world" but does not return a value

第一个函数 printAndCount,输出一个字符串并返回 Int 类型的字符数。第二个函数 printWithoutCounting调用了第一个函数,但是忽略了它的返回值。当第二个函数被调用时,消息依然会由第一个函数输出,但是返回值不会被用到。


注意:
返回值可以被忽略,但定义了有返回值的函数必须返回一个值,如果在函数定义底部没有返回任何值,这叫导致编译错误(compile-time error)。
 

多重返回值函数(Functions with Multiple Return Values)

你可以用元组(tuple)类型让多个值作为一个复合值从函数中返回。

下面的这个例子中,count 函数用来计算一个字符串中元音,辅音和其他字母的个数(基于美式英语的标准)。


func count(string: String) -> (vowels: Int, consonants: Int, others: Int) {
    var vowels = 0, consonants = 0, others = 0
    for character in string {
        switch String(character).lowercaseString {
        case "a", "e", "i", "o", "u":
            ++vowels
        case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
          "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
            ++consonants
        default:
            ++others
        }
    }
    return (vowels, consonants, others)
}

你可以用 count 函数来处理任何一个字符串,返回的值将是一个包含三个 Int 型值的元组(tuple):


let total = count("some arbitrary string!")
println("(total.vowels) vowels and (total.consonants) consonants")
// prints "6 vowels and 13 consonants"

需要注意的是,元组的成员不需要在函数中返回时命名,因为它们的名字已经在函数返回类型有有了定义。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务