Reference from here: Set default variable parameter constant parameter variable parameter input and output parameters for external parameters in swift language
Catalogue [-]
- 7.4.4 setting default values for external parameters
- 7.4.5 variable Parameters
- 7.4.6 constant parameters and variable parameters
- 7.4.7 Input-Output parameters
7.4.4 setting default values for external parameters
Developers can also set default values for external parameters. At this point, when calling, you can also omit the arguments passed this article selected from Swift1.2 Language QuickStart v2.0.
The code below example 7-11 sets the default parameter "Swift" and "---" for the external parameter ToString, Withjoiner. The code is as follows:
Import Foundation
Func Join (String s1:string, toString s2:string= "Swift", Withjoiner joiner:string= "---") {
println ("\ (S1) \ (joiner) \ (s2)")
}
Join (string: "Hello")
Since ToString and Withjoiner already have default values set, the value of two arguments is omitted when called. The results of the operation are as follows:
7.4.5 variable Parameters
With variable parameters, you can have one parameter accept values of 0 or more of the specified type. Setting a mutable parameter requires adding "..." after the parameter type name this article is selected from the Swift1.2 language QuickStart v2.0.
The code under "Example 7-12" uses a mutable parameter to calculate the arithmetic mean of any number. The code is as follows:
The results of the operation are as follows:
There are a few things to keep in mind when using variable parameters:
1. Only one variable parameter
In a function, a mutable parameter can only have one. Otherwise, an error occurs. The following code:
Two variable parameters number and AA appear in a function, causing the program to have the following error message:
2. Variable parameters must be placed in the last
Variable parameters must be placed after all parameters at all times, i.e., as the last parameter. Otherwise, an error occurs, such as the following code:
The variable parameter numbers is placed before the AA parameter in this code, causing the program to appear with the following error message:
7.4.6 constant parameters and variable parameters
In a function, the parameters are constants by default. If you want to change the value of a function parameter, it can 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 program appears with the following error message
If you want to modify a parameter, you must use a variable-like parameter, which Swift calls the variable parameter. The definition of a variable parameter is to use a var keyword before the parameter name. The following code is the ability to right-align strings using variable parameters. The code is as follows:
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 of the operation are as follows:
Note: Variable parameters can only exist in the life cycle of a function call.
7.4.7 Input-Output parameters
The parameters described above can only be changed within the function itself. For example, the following code, you want to achieve a and B values exchange. The code follows this article selected from Swift1.2 Language QuickStart v2.0:
Import Foundation
Func Swaptwoint (Var number1:int, var number2:int) {
Implements 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 ("Pre-swap")
println ("a=\ (a)")
println ("b=\ (b)")
println ("After Exchange")
Swaptwoint (A, B)
println ("a=\ (a)")
println ("b=\ (b)")
The results of the operation are as follows:
Before Exchange
After Exchange
function Body: a=3
function Body: b=2
a=2
B=3
As you can see in the run results, 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 wants to make these changes valid after the function call. At this point, you need to define the input-output parameters. It is defined by adding the InOut keyword before the name of the parameter. Its grammatical form is as follows:
The input-output parameter has a value that is passed to the function, modifies the function, and then returns from the function to replace the original value. Its invocation form is as follows:
Where the parameters precede the & operator.
The code below "Example 7-13" uses the input and output parameters to exchange functions for values A and B. The code is as follows:
Import Foundation
Func swaptwoint (inout number1:int, inout number2:int) {
Implements the exchange of two integers
Let temp = number1
Number1 = number2
Number2 = Temp
}
var a=2
var b=3
println ("Pre-swap")
println ("a=\ (a)")
println ("b=\ (b)")
println ("After Exchange")
Swaptwoint (&a, &b)
println ("a=\ (a)")
println ("b=\ (b)")
The results of the operation are as follows:
Before Exchange
After Exchange
Note: If the parameter of a function is an input-output parameter, you must add a "&" symbol to the argument before the parameter is called, or the program will get an error. As in the following code, the "&" symbol is not added before the parameter when calling a function with input and output. The code is as follows:
Since the "&" symbol was not added before the parameter in the function, the following error occurred in the program this article is selected from the Swift1.2 language QuickStart v2.0:
Note: There is a new attribute "@noescape" in Swift 1.2 that can be used in a function, which means that the parameter is the only one that can be called (or used as a parameter in a function call), meaning that its life cycle is shorter than the period of a function call, This helps with some small performance optimizations this article is selected from the Swift1.2 language QuickStart v2.0.
? Set default, variable, constant, variable, input and output parameters for external parameters in swift language