Swift - 自定义函数规则说明
2016-06-23 15:27
288 查看
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground" // 【自定义函数规则说明】 // 1.无返回值的函数 func test(name: String){ } // 2.返回一个返回值 func test2(name: String) -> Bool { return true } // 3.返回由多个值组合成的复合返回值 func test3(name: String) -> (Int,Bool) { let position = 1 let visiable = false return (position,visiable) } // 4.可变形参: 可以接受0个或者任意数量的输入参数 func test4(numbers: Int...) -> Int { var count: Int = 0 for number in numbers { count += number } return count } // 6.如果想要同时改变函数内外的参数值,可以利用inout关键字,同时调用函数的时候给参数加上前缀"&" var age = 22 func add(inout age: Int){ age += 1 } add(&age) print(age) //23 // 7.可以使用函数类型的参数 func additive(a: Int, b:Int) -> Int { return a + b } // 函数类型的参数 func printAdditiveResult(addFun: (Int,Int) -> Int, a:Int, b:Int) { print("Result:\(addFun(a,b))") } printAdditiveResult(additive, a: 5, b: 7) // 8.也可以使用函数类型的返回值 // 定义自增函数 func increase(input:Int) -> Int { return input + 1 } // 定义自减函数 func reduce(input:Int) -> Int { return input - 1 } // 定义一个返回函数类型的函数 func chooseFunction(backwards:Bool) -> (Int) -> Int { return backwards ? reduce : increase } // 测试 let aFun = chooseFunction(3 > 2) print(aFun(3)) // 2
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法