Special case of the Swift 1.1 language function parameter local parameter name external parameter name 7.4 function parameter special case
When a declaration defines an argument function, a parameter name is defined for each parameter of the function. Depending on the form defined by the parameter name, the function parameter includes both local and external parameter names, which are easy to get started with from Swift.
7.4.1 Local parameter name
The local parameter name is the name of the parameter that is written in the argument list when the function name is defined, and it can only be used within the body of the function. As one of the following code snippets, it defines a function called fun, which is defined in the parameter list of the function as a simple way to get started from Swift.
- Func Fun (start:int,end:int,str:string) {
- ......
- }
In the above code, the defined parameter names start, end, and str are local parameter names. It can only be used in the code of the function itself and cannot be used at the time of invocation.
7.4.2 External Parameter name
In a function, if the purpose of each parameter is not clear or ambiguous, it is difficult for other developers to understand the purpose of the parameter. For example, the following code, which is a function with a string connection function, the following code is simple to get started with Swift:
- Func Join (s1:string, s2:string, s3:string) {
- println ("\ (S1) \ (s3) \ (s2)")
- }
- Join ("Hello", "World", ",")
For such a function, parameters s1, S2, S3 only know that it is three strings, but it is not easy to understand the specific purpose of each parameter. To address this challenge, Swift provides an external parameter name. The external and local parameters are defined as the same. But if the developer needs to better express the function of the parameter, it needs to use the external parameter name when defining it. The external parameter name needs to be written before the local parameter name and separated with a space. Its general form is as follows:
- Func function name (external parameter name local parameter name: data type) {
- ......
- }
For functions with external parameter names, the calling form is as follows:
- function name (external parameter name: parameter value)
Example 7-7 The following example defines an external parameter for a function that has a string join function. The code is as follows:
- Func Join (string s1:string,tostring s2:string, joiner s3:string) {
- println ("\ (S1) \ (s3) \ (s2)")
- }
- Join (string: "Hello", ToString: "Swift", Joiner: ",")
- where string, tostring, and joiner are the names of external parameters. The results of the operation are as follows:
- Hello,swift
- Program ended with exit code:0
Here are the three points to note:
(1) Use an external parameter when calling: if you provide an external parameter name for the parameter, you need to use the outer parameter name while calling the function. Otherwise, the program will prompt for errors. For example, the following function call code with string join function is simple to get started with Swift:
- Join ("Hello", "Swift", ",")
Because the external parameter name string, tostring, joiner are not used in the code above, the following error message appears:
- Missing argument labels ' string:tostring:joiner: ' in call
(2) Order of parameters: using an external parameter name can be a good idea for other developers to understand the purpose of each parameter, but the order of invocation must also be consistent with defining the declaration function. For example, the following code calls a function with the string join function:
- Join (ToString: "Swift", String: "Hello", Joiner: ",")
When declaring a function, the order of the arguments is (string,tostring, joiner), and the order is (ToString, String,joiner) at the time of invocation. Because the order is different, the program has the following error message:
- Argument ' string ' must precede Argument ' tostring '
(3) A quick way to write the name of an external parameter: If the developer wants to provide an external parameter name for the parameter of the function, but the local parameter name already uses a suitable name, then it is no longer necessary for the parameter to be written the same two times name. Instead, write the name once and prefix the name with a "#" number. It tells the compiler that this name is used as the local parameter name and the external parameter name. Its general form is as follows:
- Func function name (#本地参数名: Data type) {
- ......
- }
"Example 7-8" The following program implements the string connection function, and the definition of the external parameter name uses the "#" sign. The following code from Swift is easy to get started with:
- Import Foundation
- Func Join (#string: String, #tostring: String, #withJoiner: String) {
- println ("\ (string) \ (Withjoiner) \ (tostring)")
- }
- Join (string: "Hello", ToString: "World", Withjoiner: ",")
Although you can use the "#" shorthand when defining, you must write the full external parameter name when calling.
7.4.3 Setting parameters default values
When defining a function, you can set a default value for the parameter. This way, the value of the parameter is not passed when it is called.
The code below "Example 7-9" defines the default values of 0 and 5, respectively, for the start and end parameters, and the function is to output a value of 0 to 5. The code is as follows:
- Import Foundation
- Declaration definition function
- Func Fun (start:int=0,end:int=5) {
- var I=start
- For i;i<=end;++i{
- println (i)
- }
- }
- Fun () //Call
In the code, the function fun () call did not pass any parameter values. This is because the parameter start and end have default values when the function fun () is defined, so the arguments can be passed without passing the call. The results of the operation are as follows. Getting started with Swift is simple:
- 0
- 1
- 2
- 3
- 4
- 5
- Program ended with exit code:0
Note: The following points need to be noted when using the default values:
(1) When defined, the parameter that uses the default value is placed at the end of the function's argument list. This ensures that when the function is called, the order of the parameters that do not have a default value is not affected by omitting the value of the parameter.
(2) When called, if the default value of the parameter has already been set, you can omit the value of the argument.
(3) If the developer sets a default value, but wants to change the default value at the time of the call, you can continue to assign the value.
"Example 7-10" The following code outputs a number from 3 to 8, including 3 and 8. The code is as follows:
- Import Foundation
- Func Fun (start:int=0,end:int=3) {
- var I=start
- For i;i<=end;++i{
- println (i)
- }
- }
- println ("Use default value range")
- Fun ()
- println ("Use changed range")
- Fun (Start:3,end:8) //Change the default value
In the code, the first call uses the default value of the parameter, and the second call changes the default value of the parameter. The results of the operation are as simple as getting started with Swift:
- Using the default range of values
- 0
- 1
- 2
- 3
- Use the changed range
- 3
- 4
- 5
- 6
- 7
- 8
- Program ended with exit code:0
When you set a default value for a parameter, Swift automatically provides the parameter with the same name as the external parameter. If you need to change the default value when calling, you must use an external parameter and you cannot assign a value directly. Otherwise, an error occurs, such as the following code:
In this code, start and end are assigned directly, so a program error is caused, and its error message is as follows: The following is a simple entry from Swift.
- Missing argument labels ' start:end: ' in call
Special case for Swift 1.1 language function parameters local parameter name external parameter name