Swift学习:12.函数

Swift学习:12.函数,第1张

概述1.函数的定义与调用 func sayHello(personName: String) -> String {     let greeting = "Hello, " + personName + "!"     return greeting } 2.外部参数 func someFunction(externalParameterName localParameterName: Int) { 1.函数的定义与调用

func sayHello(personname: String) -> String {

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

return greeting

}

2.外部参数

func someFunction(externalParametername localParametername: Int) {}

func containsCharacter(#string: String,#characterToFind: Character) -> Bool {}


3.可变参数

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)

arithmeticmean(3,8,19)


4.默认参数

func join(string s1: String,toString s2: String,withJoiner joiner: String = " ") -> String {

return s1 + joiner + s2

}

join(string: "hello",toString: "world")


5.输入输出参数

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

let temporaryA = a

a = b

b = temporaryA

}

var someInt = 3

var anotherInt = 107

swapTwoInts(&someInt,&anotherInt)


6.函数类型

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() {

println("hello,world")

}

这个函数的类型是() -> (),读作“没有参数,并返回 VoID 类型的函数”

7.函数类型作为参数

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

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

}

printMathResult(addTwoInts,5)

当printMathResult 被调用时,它被传入 addTwoInts 函数和整数3和5。它用传入3和5调用 addTwoInts,并输出结果:8。


8.嵌套函数

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

}

9.示例代码

func sayHello(#username:String,age userAge:Int = 20)->String{

return "用户名:\(username) 年龄:\(userAge)"

}

func getname()->(name:String,age:Int){

println("No return")

return ("sn",24)

}

func countPerson(names:String...){

for name in names{

println(name)

}

}

//println(sayHello("sn",24))

//sayHello(name: "sn",age: 24)

//sayHello(username: <#String#>,age: <#Int#>)

println(sayHello(username: "sn"))

var people = getname()

println(people.name)

countPerson("Helen","Tom","Jike")


var myname = "sn"


func changename(inout name:String){

name = name + "AAA"

}

changename(&myname)

println(myname)


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

let num = countNum - count(string)

if num < 1{

return string

}

else{

for _ in 1...num{

string = pad + string

}

return string

}

}


let originalString = "hello"

let paddedString = alignRight(originalString,10,"-")

println(paddedString)


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

return a+b

}

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

var mathFunction = addTwoInts

var num = mathFunction(2,3)

println(num)


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

println("result is \(mathFunction(a,b))")

}

printMathResault(mathFunction,5)


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 value = -34

let moveNearToZero = chooseStepFunction(value>0)

while value != 0{

println("current value is \(value)")

value = moveNearToZero(value)

}

总结

以上是内存溢出为你收集整理的Swift学习:12.函数全部内容,希望文章能够帮你解决Swift学习:12.函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存