Swift 笔记(五)
来源:互联网 发布:sql语句group by的用法 编辑:程序博客网 时间:2024/06/05 12:01
我的主力博客:半亩方塘
Functions
1、With two parameters, the function can print out a multiple of any two values. You could achieve this like so:
func printMultipleOf(multiplier: Int, andValue: Int) { print("\(multiplier) * \(andValue) = \(multiplier * andValue)")}printMultipleOf(4, andValue: 2)
You pass in two values when you call the function. Notice that the second parameter has a label before it. This syntax is Swift's way of letting you write code that reads like a sentence. The first parameter to a function has no label in the function call. All subsequent parameters are labeled with their names. In example above, you would read the last line of code like this:
Print multiple of 4 and value 2
You can make this even clearer by giving a parameter a different external name. For example, you can change the external name of the andValue
parameter:
func printMultipleOf(multiplier: Int, and andValue: Int) {print("\(multiplier) * \(andValue) = \(multiplier * andValue)")}printMultipleOf(4, and: 2)
You assign a different external name by writing it in front of the parameter name. In this example,andValue
is still the name of the parameter, but the label in the function call is now simply and
. You can read the new call as:
Print multiple of 4 and 2
If you want to have no external name at all, then you can employ the underscore _
:
func printMultipleOf(multiplier: Int, _ andValue: Int) {print("\(multiplier) * \(andValue) = \(multiplier * andValue)")}printMultipleOf(4, 2)
In this example, the second parameter has no external name, just like the first parameter. But use the underscore wisely. Here, your expression is still understandable, but more complex functions that take many parameters can become confusing and unwieldy with no external parameter names.
2、You can also give parameters default values:
func printMultipleOf(multiplier: Int, and andValue: Int = 1) {print("\(multiplier) * \(andValue) = \(multiplier * andValue)")}printMutipleOf(4)
The difference is the = 1
after the second parameter, which means that if no value is given for the second parameter, it defaults to 1.
3、Parameters passed to functions are constants by default, which means they can't be modified.
4、Sometimes you do want to let a function change a parameter directly, a behavior called pass by reference. You do it like so:
func incrementAndPrintInOut(inout value: Int) { value++ print(value)}
The inout
keyword before the parameter name indicates that this parameter should use pass-by-reference. Now you need to make a slight tweak to the function call:
var value = 5incrementAndPrintInOut(&value)print(value)
You add an ampersand(&) before the parameter, which helps you remember that the parameter is using pass-by-reference. Now the function can change the value however it wishes.
This example will print the following:
6
6
5、Functions in Swift are simply another data type. You can assign them to variables and constants just as you can any other type of value, like an Int
or a String
.
func add(a: Int, _ b: Int) -> Int { return a + b}
You can assign this function to a variable, like so:
var function: (Int, Int) -> Int = addNow you can use the
function
variable in just the same way you'd use add
, like so:let result = function(4, 2)
In this example, result
equals 6.
You can also pass functions to other functions. Here's an example of this in action:
func printResult(function: (Int, Int) -> Int, _ a: Int, _ b: Int) { let result = function(a, b) print(result)}printResult(add, 4, 2)
Then it prints the result to the console:
6
- Swift 笔记(五)
- Swift学习笔记 (五)
- swift学习笔记(五)
- Swift语言学习笔记(五)
- swift 学习笔记五
- Swift 笔记 五:控制流
- swift自学笔记(五)(重写属性、final)
- 基本运算符—Swift学习笔记(五)
- Swift学习笔记(五)—— 学习心得
- swift UITableView(五)
- Swift数据类型(五)
- Swift 笔记 (五) —— 控制流
- swift学习笔记(五)构造过程
- swift语言的学习笔记五(线程)
- 自学Swift-斯坦福笔记整理(五)
- Swift学习笔记(五)闭包
- iOS学习笔记45-Swift(五)协议
- swift语言的学习笔记五(线程)
- OpenGL之坐标转换(好文-清晰版)
- spring data jap的openEntityManagerInViewFilter和hibernate的openSessionInViewFilter
- 从今天开始坚持写博客
- Android即时通讯--仿QQ即时聊天:(五)聊天模块
- Java在linux下调用C/C++生成的so文件
- Swift 笔记(五)
- 译:在ASP.NET MVC5中如何使用Apache log4net 类库来记录日志
- 学习Kotlin笔记记录。--------类
- 大型网站架构系列:分布式消息队列(一)
- 1087. All Roads Lead to Rome (30)
- Android Fragment 生命周期图
- VMware Workstation 网络设置解释三种
- 第二天:图像标签及超链接的使用
- Python——异常编码细节