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 = add  
Now 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






0 0
原创粉丝点击