Default variable parameter constant parameter variable parameter input and output parameters for external parameters in swift language
Source: Internet
Author: User
label:
Setting default values for external parameters in Swift
Developers can also set default values for external parameters. At this time, when calling, you can also omit parameter passing. This article is selected from the Swift 1.2 language quick start v2.0.
[Example 7-11] The following code sets the default parameters "Swift" and "---" for the external parameters toString and withJoiner. code show as below:
Because toString and withJoiner have already set the default values, the passing of the two parameters is omitted when calling. The results are as follows:
Hello --- Swift
7.4.5 Variable parameters
Using variable parameters allows a parameter to accept zero or more values of a specified type. Setting a variable parameter requires adding "..." after the parameter type name. This article is selected from the Swift 1.2 language quick start v2.0.
[Example 7-12] The following code uses a variable parameter to calculate the arithmetic mean of any number. code show as below:
import Foundation
func average (numbers: Double ...) {
var total: Double = 0
for number in numbers {
total + = number
}
println (total / Double (numbers.count))
}
average (1, 2, 3, 4, 5)
average (3, 8, 19)
The results are as follows:
3.0
10.0
When using variable parameters, there are a few things to note:
1. Only one variable parameter
There can be only one variable parameter in a function. Otherwise, an error will occur. The following code:
func average (numbers: Double ..., aa: Int ...) {
...
}
Two variable parameters number and aa appeared in a function, causing the program to display the following error message:
‘...’ must be on the last parameter
2. Variable parameters must be placed last
Variadic parameters must be placed after all parameters at all times, as the last parameter. Otherwise, an error will occur, as follows:
func average (numbers: Double ..., aa: Int) {
...
}
In this code, the variable parameter numbers is placed before the aa parameter, causing the program to display the following error message:
‘...’ must be on the last parameter
7.4.6 Constant and Variable Parameters
In functions, parameters are constants by default. If you want to change the value of a function parameter, it will cause a program error. Such as the following code:
func fun (num: Int) {
num = num + 1
println (num)
}
In this code, the function parameter num is a constant by default, and the value of the constant cannot be changed, so the following error message appears in the program
Cannot assign to ‘let’ value ‘num’
If you want to modify a parameter, you must use a variable-like parameter, which Swift calls a variable parameter. The definition of a variable parameter is to use a var keyword before the parameter name. The following code uses the variable parameter to achieve the right-justification of the string. code show as below:
import Foundation
func alignRight (var string: String, cou: Int, pad: String) {
var sc = count (string)
let amountToPad = cou-sc // Get the number of input "*"
// traverse
for _ in 1 ... amountToPad {
string = pad + string
}
println (string)
}
let originalString1 = "swift"
alignRight (originalString1, 10, "*")
let originalString2 = "Hi"
alignRight (originalString2, 10, "*")
let originalString3 = ""
alignRight (originalString3, 10, "*")
The results are as follows:
***** swift
******** Hi
**********
Note: Variable parameters can only exist during the life cycle of a function call.
7.4.7 Input-output parameters
The parameters mentioned above can only be changed within the function itself. For example, the following code wants to exchange the values of a and b. The code is as follows. This article is selected from the Swift 1.2 language quick start v2.0:
import Foundation
func swapTwoInt (var number1: Int, var number2: Int) {
// Implement the exchange of two integers
let temp = number1
number1 = number2
number2 = temp
println ("Function body: a = \ (number1)")
println ("Function body: b = \ (number2)")
}
var a = 2
var b = 3
println ("before exchange")
println ("a = \ (a)")
println ("b = \ (b)")
println ("after swapping")
swapTwoInt (a, b)
println ("a = \ (a)")
println ("b = \ (b)")
The results are as follows:
Before the exchange
a = 2
b = 3
After the exchange
Function body: a = 3
Function body: b = 2
a = 2
b = 3
It can be seen in the running results that the values of a and b can be exchanged within the function itself. If the developer wants to use a function to modify the value of the parameter, and want to make these changes still effective after the function call. In this case, you need to define the input-output parameters. It is defined by adding the inout keyword before the parameter name. Its syntax is as follows:
func function name (inout parameter name: data type, ...) {
...
}
The input-output parameters have a value passed to the function. After the function is modified, it returns from the function to replace the original value. Its calling form is as follows:
Function name (& parameter, ...)
Among them, the parameter is preceded by the & operator.
[Example 7-13] The following code uses the input and output parameters to implement the exchange function of a and b values. code show as below:
import Foundation
func swapTwoInt (inout number1: Int, inout number2: Int) {
// Implement the exchange of two integers
let temp = number1
number1 = number2
number2 = temp
}
var a = 2
var b = 3
println ("before exchange")
println ("a = \ (a)")
println ("b = \ (b)")
println ("after swapping")
swapTwoInt (& a, & b)
println ("a = \ (a)")
println ("b = \ (b)")
The results are as follows:
Before the exchange
a = 2
b = 3
After the exchange
a = 3
b = 2
Note: If the parameter of a function is an input-output parameter, you must add an "&" symbol before the parameter when calling, otherwise the program will get an error. As shown in the following code, when a function with input and output is called, the "&" symbol is not added before the parameter. code show as below:
import Foundation
func swapTwoInt (inout number1: Int, inout number2: Int) {
...
}
var a = 2
var b = 3
...
swapTwoInt (a, b)
...
Because the "&" symbol was not added before the parameter when the function was called, the following error occurred in the program.
Passing value of type ‘Int’ to an inout parameter requires explicit ‘&’
Note: In Swift 1.2, there is a new attribute "@noescape" that can be used in functions, which means that this parameter is the only one that can be called (or used as a parameter when the function is called), which means it The life cycle is shorter than the function call cycle, which helps some small performance optimizations. This article is selected from the Swift 1.2 language quick start v2.0.
Setting default values for external parameters in Swift
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.