加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 笔记(五)

发布时间:2020-12-14 07:14:41 所属栏目:百科 来源:网络整理
导读:我的主力博客:半亩方塘 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) = (m

我的主力博客:半亩方塘


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 thatthe 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 theandValueparameter:

func printMultipleOf(multiplier: Int,and andValue: Int) {
print("(multiplier) * (andValue) = (multiplier * andValue)")
}

printMultipleOf(4,and: 2)  

You assign a different external name by writing itin front of the parameter name. In this example,andValueis still the name of the parameter,but the label in the function call is now simplyand. 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= 1after 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 calledpass by reference. You do it like so:

func incrementAndPrintInOut(inout value: Int) {
    value++
    print(value)
}

Theinoutkeyword 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 = 5
incrementAndPrintInOut(&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 anIntor aString.

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 = add  
Now you can use the function variable in just the same way you'd use add ,like so:

let result = function(4,2)  

resultequals 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读