swift中函数的形参和返回值

swift 中,函数的形参和返回值是非常具有灵活性的。你可以定义任何事情,无论是一个简单的仅有一个未命名形参的工具函数,还是那种具有丰富的参数名称和不同的形参选项的复杂函数

多输入形参

1. func halfOpenRangeLength(start: Int,end: Int) -> Int { 2. return end - start
3. }
4. println(halfOpenRangeLength(1,10))
5. // prints "9"

无形参函数

1. func sayHelloWorld() -> String { 2. return "hello,world"
3. }
4. println(sayHelloWorld())
5. // prints "hello,world"

无返回值的函数

1. func sayGoodbye(personName: String) {
2. println("Goodbye,\(personName)!")
3. }
4. sayGoodbye("Dave")
5. // prints "Goodbye,Dave!"

多返回值函数

你可以使用一个元组类型作为函数的返回类型,来返回一个由多个值组成的复合返回值。
1. func count(string: String) -> (vowels: Int,consonants: Int,others: I nt) {
2. var vowels = 0,consonants = 0,others = 0
3. for character in string {
4. switch String(character).lowercaseString {
5. case "a","e","i","o","u":
6. ++vowels
7. case "b","c","d","f","g","h","j","k","l","m",8. "n","p","q","r","s","t","v","w","x","y","z":
9. ++consonants
10. default:
11. ++others
12. }
13. }
14. return (vowels,consonants,others) 15. }
参考:《The Swift Programming Language中文完整版(CocoaChina精校)》

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...