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