《swift2.0 官方教程中文版》 第2章-06函数


import Foundation


/*函数的定义与调用*****************************************************/

//在下面例子中的函数叫做 "sayHello(_:)",之所以叫这个名字,是因为这个函数一个人的名字当做输入,并返回 给这个人的问候语。为了完成这个任务,你定义一个输入参数-一个叫做 personName String ,一个 包含给这个人问候语的 String 类型的返回值:

func sayHello(personName: String) -> String {

let greeting = "Hello," + personName + "!"

return greeting

}

//所有的这些信息汇总起来成为函数的定义,并以 func 作为前缀。指定函数返回类型时,用返回箭头 -> (一个 连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。


print(sayHello("Anna"))

// prints "Hello,Anna!"

print(sayHello("Brian"))

// prints "Hello,Brian!"


//为了简化这个函数的定义,可以将问候消息的创建和返回写成一句:

func sayHelloAgain(personName: String) -> String {

return "Hello again," + personName + "!"

}

print(sayHelloAgain("lll"))

// prints "Hello again,Anna!"




/*函数参数与返回值*****************************************************/


// 多重输入参数

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

func halfOpenRangeLength(start: Int,end: Int) -> Int {

return end - start

}

print(halfOpenRangeLength(1,end: 10))

// prints "9"


// 无参函数

func sayHelloWorld() -> String {

return "Hello,world"

}

print(sayHelloWorld())

// prints "hello,world"


// 多参量函数

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 sayGoodbye(personName: String) {

print("Goodbye,\(personName)")

}

sayGoodbye("Dave")

// prints "Goodbye,Dave!"


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


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

func printAndCount(stringToPring: String) -> Int {

print(stringToPring)

return stringToPring.characters.count

}

func printWithoutCounting(stringToPoint: String) {

printAndCount(stringToPoint)

}

printAndCount("hello,world")

// prints "hello,world" and returns a value of 12

printWithoutCounting("hello,world" but does not return a value


//多重返回值函数

//下面的这个例子中,定义了一个名为 minMax(_:) 函数,作用是在一个 Int 数组中找出最小值与最大值.

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)

}

print(minMax([5,2,123,4,576,23]))


let bounds = minMax([8,-6,109,3,71])

print("min is \(bounds.min) and max is \(bounds.max)")

// prints "min is -6 and max is 109"


//为了安全的处理这个"空数组"问题,一个 minMax(_:) 函数使用可选元组返回类型,并且当数组为空时返回 nil :

func minMax2(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)

}

//你可以选择性的绑定当 minMax(_:) 函数返回的是一个实际的元组值还是 nil

if let bounds = minMax2([8,71]) {

print("min is \(bounds.min) and max is \(bounds.max)")

}

// prints "min is -6 and max is 109"




/*函数参数名称*****************************************************/

//函数参数都有一个外部参数名(external parameter name)一个本地参数名(local parameter name).外部参数名用来标记传递给函数调用的参数,本地参数名在实现函数的时候使用.

func someFunction(FirstParameterName: Int,secondParameterName: Int) {

// function body goes here

// firstParameterName and secondParameterName refer to

// the argument values for the first and second parameters

}

someFunction(1,secondParameterName: 2)


//你可以在本地参数名前指定外部参数名,中间以空格分隔.

func someFunction(externalParameterName localParameterName: Int) {

// function body goes here,and can use localParameterName

// to refer to the argument value for that parameter

}


//注意: 如果你提供了外部参数名,那么函数在被调用,必须使用外部参数名。

func sayHello(to person: String,and anotherPerson: String) -> String {

return "Hello \(person) and \(anotherPerson)"

}

print(sayHello(to: "Bill",and: "Ted"))

// prints "Hello Bill and Ted!"


//如果你不想为第二个及后续的参数设置参数名,一个下划线(_)代替一个明确地参数名.

func someFunction2(firstParameterName: Int,_ secondParameterName: Int) {

// function body goes here

// firstParameterName and secondParameterName refer to

// the argument values for the first and second parameters

}

someFunction2(1,2)

//注意: 因为第一个参数认忽略其外部参数名称,明确写下划线是多余的。


//你可以在函数体中为每个参数定义 认值(Deafult Values) 。当认值被定义后,调用这个函数时可以忽略这个 参数。

func someFunction3(parameterWithDefault: Int = 12) {

print("参数是\(parameterWithDefault)")

}

someFunction3(6)

someFunction3()

//注意: 将带有认值的参数放在函数参数列表的最后。这样可以保证在函数调用,认参数的顺序是一致 ,同时使得相同的函数在不同情况下调用时显得更为清晰。



//一个 可变参数(variadic parameter) 可以接受零个或多个值。函数调用,你可以用可变参数来传入不确定数量的输入参数。通过在变量类型名后面加入 (...) 的方式来定义可变参数。

//下面的这个函数用来计算一组任意长度数字的 算术平均数(arithmetic mean) :

func arithmeticmean(numbers: Double...) -> Double {

var total: Double = 0

for number in numbers {

total += number

}

return total / Double(numbers.count)

}

print(arithmeticmean(1,2,3,4,5))

print(arithmeticmean(3,8.25,18.75))

//注意: 最多可以有一个可变参数函数,和它必须出现在参数列表中,为了避免歧义在调用函数有多个参数。 如果你的函数一个或多个参数有认值,还有一个可变的参数,将可变参写在参数列表的最后。


//常量参数和变量参数(Constant and Variable Parameters)

//通过在参数名前加关键字 var 来定义变量参数

func alignRight(var string: String,totalength: Int,pad: Character) -> String {

let amountToPad = totalength - string.characters.count

if amountToPad < 1 {

return string

}

let padString = String(pad)

for _ in 1...amountToPad {

string = padString + string

}

return string

}

let originalString = "hello"

let paddedString = alignRight(originalString,totalength: 10,pad: "-")

print("originalString is \(originalString) and paddedString is \(paddedString)")


//输入输出参数(In-Out Parameters)

//注意: 输入输出参数不能有认值,而且可变参数不能用 inout 标记。如果你用 inout 标记一个参数,这个参数不能被 var 或者 let 标记

func swapTwoInts(inout a: Int,inout _ b: Int) {

let temporaryA = a

a = b

b = temporaryA

}

//这个 swapTwoInts(_:_:) 函数仅仅交换 a b 的值。该函数先将 a 的值存到一个暂时常量 temporaryA ,然后将 b 的值赋给 a,最后将 temporaryA 幅值给 b

//你可以用两个 Int 型的变量来调用 swapTwoInts(_:_:) 。需要注意的是,someInt anotherInt 在传入 s wapTwoInts(_:_:) 函数,都加了 & 的前缀:

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"


//注意: 输入输出参数和返回值是不一样的。上面的 swapTwoInts 函数并没有定义任何返回值,但仍然修改 someInt anotherInt 的值。输入输出参数是函数函数体外产生影响的另一种方式。




/*函数类型*****************************************************/

//每个函数都有种特定的函数类型,函数的参数类型和返回类型组成。

func addTwoInts(a: Int,_ b: Int) -> Int {

return a + b

}

func multiplyTwoInts(a: Int,_ b: Int) -> Int {

return a * b

}

//这两个函数的类型是 (Int,Int) -> Int,可以读作这个函数类型,它有两个 Int 型的参数并返回一个 Int 型的 值。

//下面是另一个例子,一个没有参数,也没有返回值的函数:

func printHelloWorld() {

print("hello,world")

}

//这个函数的类型是: () -> void,或者叫没有参数,并返回 Void 类型的函数


// Swift ,使用函数类型就像使用其他类型一样。例如,你可以定义一个类型为函数的常量或变量,并将函数 赋值给它

var mathFunction: (Int,Int) -> Int = addTwoInts

//这个可以读作:“定义一个叫做 mathFunction 的变量,类型是一个有两个 Int 型的参数并返回一个 Int 型的值的函数’,并让这个新变量指向 addTwoInts 函数

print("Result: \(mathFunction(2,3))")


//有相同匹配类型的不同函数可以被赋值给同一个变量,就像非函数类型的变量一样:

mathFunction = multiplyTwoInts

print("Result: \(mathFunction(2,3))")

// prints "Result: 6"


//下面是另一个例子,正如上面的函数一样,同样是输出某种数学运算结果:

func printMathResult(mathFunction: (Int,Int) -> Int,_ a: Int,_ b: Int) {

print("Result: \(mathFunction(a,b))")

}

printMathResult(addTwoInts,5) // prints "Result: 8"


//printMathResult(_:_:_:) 函数的作用就是输出一个合适类型的数学函数调用结果。它不关心传入函数是如 何实现的,它只关心这个传入的函数类型是正确的。这使得 printMathResult(_:_:_:) 可以以一种类型安全(type-safe)的方式来保证传入函数调用是正确的。


//你可以用函数类型作为另一个函数的返回类型。你需要做的是在返回箭头( -> )后写一个完整的函数类型。

//下面的这个例子中定义了两个简单函数,分别是 stepForward stepBackward stepForward 函数返回一个比输入值大一的值。 stepBackward 函数返回一个比输入值小一的值。这两个函数的类型都是 (Int) -> Int :

func stepForward(input: Int) -> Int {

return input + 1

}

func stepBackward(input: Int) -> Int {

return input - 1

}

//下面这个叫做 chooseStepFunction(_:) 函数,它的返回类型是 (Int) -> Int 函数 chooseStepFunction(_:) 根据布尔值 backwards 来返回 stepForward(_:) 函数 stepBackward(_:) 函数:

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

print("Counting to zero:")

// Counting to zero:

while currentValue != 0 {

print("\(currentValue)... ")

currentValue = moveNearerToZero(currentValue)

}

print("zero!")

// 3...

// 2...

// 1...

// zero!




/*嵌套函数*****************************************************/

//这章中你所见到的所有函数都叫全局函数(global functions),它们定义在全局域中。你也可以把函数定义在别的函数体中,称作嵌套函数(nested functions)

func chooseStepFunction2(backwards: Bool) -> (Int) -> Int {

func stepForward2(input: Int) -> Int {return input + 1}

func stepBackward2(input: Int) -> Int {return input - 1}

return backwards ? stepBackward2 : stepForward2

}

var currentValue2 = -4

let moveNearerToZero2 = chooseStepFunction2(currentValue2 > 0)

// moveNearerToZero Now refers to the nested stepForward() function

while currentValue2 != 0 {

print("\(currentValue2)...")

currentValue2 = moveNearerToZero2(currentValue2)

}

print("zero!")

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...