Swift 笔记(五)
我的主力博客:半亩方塘
Functions1、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 the 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, 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 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) } The 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. 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
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) 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |