Swift  函数

//函数
//使用func定义一个函数.调用函数使用它的名字加上小括号中的参数列表. 使用->分隔参数的名字和返回值类型
//函数声明
func greet(name:String, day:String) -> String {
    return "hello \(name), today is \(day)"
}


//函数调用
print(greet("Tom", day: "Tuesday"))
//无返回值函数
func say(personName: String) {
    print("good \(personName)")
}
say("Tom")
//多返回值函数
//使用元祖类型返回多个值
func count(string: String) -> (vow: Int, con:Int, oth: Int) {
    var vow = 0, con = 0, oth = 0
    for chara in string.characters {
        switch String(chara).lowercaseString {
            case "a" , "e" , "i":
            ++vow
            case "b" , "c", "d" , "s":
            ++con
        default:
            ++oth
        }
    }
    return (vow,con,oth)
}
let totle = count("some apple best")
print("\(totle.vow)---\(totle.con)---\(totle.oth)")
```
//嵌入函数
//返回的值相当于函数指针
func choose(backW: Bool) -> (Int) -> Int {
    func stepF(input: Int) -> Int {
        return input + 1
    }
    func stepB(input: Int) -> Int {
        return input - 1
    }
    return backW ? stepB : stepF
}
var curr = -4
let move = choose(curr > 0)

while curr != 0 {
    print("\(curr)...")
    curr = move(curr)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值