前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swift 函数

swift 函数

作者头像
赵哥窟
发布2018-09-13 11:52:32
6500
发布2018-09-13 11:52:32
举报
文章被收录于专栏:日常技术分享

定义和使用函数

下面定义名称 sayHello 的函数,只有一个 String 类型的 name 参数,函数返回值为 String 类型。

func sayHello(name:String)->String{     return "Hello,"+name }

函数参数和返回值

没有参数的函数

func helloWorld()->String{    return"Hello World" }

有多个参数的函数,参数之间用逗号分割

func helloPeople(firtName:String,lastName:String)->String{    return"Hello, "+firtName+" "+lastName }

没有返回值的函数

func printName(firtName:String,lastName:String){  print("Hello, "+firtName+" "+lastName) }

有多个返回值的函数,函数通过返回一个元组来返回多个值。

func tuplesFunction(name:String)->(hello:String,goodbye:String){ let hello="Hello, "+name let goodbye="Goodbye, "+name return(hello,goodbye) }

函数参数名称

函数的每一个参数都有外部名称和内部名称,外部名称在调用函数时使用,内部名称在函数内部实现中使用。

默认情况,函数第一个参数会忽略外部名称,后面的参数外部名称和内部名称一致,如下面的示例。

func sayHello(firtName:String,lastName:String){ print("Hello, "+firtName+" "+lastName) } sayHello("zhao",lastName:"Alex")

指定外部名称

下面示例中 firstName 和 lastName 是外部名称,first 和 last 是内部名称,如果指定了外部名称,调用函数时也要写明外部名称。

func sayHello(firtName first:String,lastName last:String){ print("Hello, "+first+" "+last) } sayHello(firtName:"zhao",lastName:"Alex")

忽略外部名称,用下划线来忽略外部名称。

func sayHello(firstName:String,_lastName:String){ print("Hello, "+firstName+" "+lastName) } sayHello("zhao","Alex")

默认参数值

函数参数可以指定默认值,在没有传入参数值时,此参数就使用默认值。

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

输入输出参数

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"

函数类型

函数类型由参数类型和返回值类型构成,如下示例的函数类型就是 (String, String) -> String

func sayHello(firtName:String,lastName:String){    print("Hello, "+firtName+" "+lastName) }

使用函数类型

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

例如:

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") }

使用函数类型

在 Swift 中,使用函数类型就像使用其他类型一样。例如,你可以定义一个类型为函数的常量或变量,并将适当

的函数赋值给它:

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

print("Result: \(mathFunction(2, 3))") // prints "Result: 5"

函数类型作为参数类型

func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {     print("Result: \(mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8"

函数类型作为返回类型

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

嵌套函数

func chooseStepFunction(backwards:Bool) ->Int{    func stepForward(input:Int) ->Int{       returninput +1    }    func stepBackward(input:Int) ->Int{         returninput -1      }   returnbackwards ?stepBackward(4) :stepForward(3) }

letcurrentValue =3 letmoveNearerToZero =chooseStepFunction(currentValue >0) print(moveNearerToZero)  // 3

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.07.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档