func sayHello(usename:String, age:Int) ->String{
print("\(usename) + \(age)")
return "RETURN \(usename) + \(age)"
}
let STR = sayHello("XIAOMING", age: 18)
print("\(STR)")
func nothingReturn(){
println("nothing")
}
nothingReturn()
func getMsg() ->(name:String, age:Int){
return ("sunning",30)
}
var tus = getMsg() //用元组接收多返回值
println(tus)
func sumOf(numbers:Int...) ->Int{
var sum = 0
for number in numbers{
sum += number
}
return sum
}
sumOf(12,23,33)
func sayGoodbye(person usename:String, age:Int) ->String{
return "用户名:\(usename) 年龄: \(age)"
}
sayGoodbye(person: "helen", 19)
func sayGoodbye2(#usename:String, age:Int) ->String{
return "用户名:\(usename) 年龄: \(age)"
}
sayGoodbye2(usename: "jack", 20)
var myName = "mike"
func chageName(inout name:String){
name += "AAA"
}
chageName(&myName)
println(myName)
func addMoney(a:Int, b:Int) ->Int{
return a+b
}
var getMoney = addMoney //getMoney指向addMoney
println(getMoney(1,2))
func onMoneyAdd((Int,Int) -> Int){
println("event called")
}
onMoneyAdd(getMoney) //函数作为入参,然后执行onMoneyAdd的操作,并且
func onMoneyDelete() ->((Int,Int) ->Int){ //只带返回值
return getMoney //当onMoneyDelete调用的时候,就会返回getMoney这个函数。
}
let moneyFunc = onMoneyDelete()
moneyFunc(1,2)
func chooseStep(backWards:Bool) -> (Int) ->Int{ //(Int) ->Int代表一个函数(有入参和返回值)
func stepForward(input:Int) ->Int{return input+1}
func stepBackWard(input:Int) ->Int{return input+1}
return backWards ? stepForward : stepBackWard
}
let currentValue = -4
let move = chooseStep(currentValue > 0) //move是stepBackWard函数
print("\(move(currentValue))")