Detailed definition
Function Parameter Names
Function parameters has both an external parameter name and a local parameter name. An external parameter name was used to label arguments passed to a function call. A local parameter name is used in the implementation of the function.
-
func somefunction (firstparametername: int, secondparametername: Int) {
-
//function Body goes here
-
//Firstparametername and Secondparametername refer to
-
//the argument values For the first and second parameters
-
}
-
somefunction (1, secondparametername: 2)
By default, the first parameter omits it external name, and the second and subsequent parameters use their local name as Their external name. All parameters must has a unique local names, but could share external parameter in common.
Specifying External Parameter Names
You write a external parameter name before the local parameter name it supports, separated by a space:
func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName
// to refer to the argument value for that parameter
}
NOTE
If you provide the external parameter name for a parameter, which external name must always being used when your call T he function.
Here's a version of the sayHello(_:) function that takes the names of both people and returns a greeting for both of them:
-
func sayhello ( to person: string, and Anotherperson: string), string {
-
return "Hello \" Span class= "VC" >person) and \ (anotherperson) ! "
-
}
-
print (sayhello (to: "Bill", and: Span class= "s" > "Ted"))
-
//prints "Hello Bill and ted!"
By specifying external parameter names for both parameters, both the first and second arguments to the sayHello(to:and:) function must Be labeled when you call it.
The use of external parameter names can-a function to being called in an expressive, sentence-like manner, while still Providing a function body is readable and clear in intent.
Omitting External Parameter Names
If you don't want to use an external name for the second or subsequent parameters of a function, write an underscore ( ) instead of a explicit external name for that parameter.
func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
// function body goes here
// firstParameterName and secondParameterName refer to
// the argument values for the first and second parameters
}
someFunction(1, 2)
NOTE
Because the first parameter omits its external parameter name by default, explicitly writing a underscore is extraneous.
Default Parameter Values
You can define a default value of parameter in a function by assigning a value to the parameter after that P Arameter ' s type. If A default value is defined, you can omit this parameter when calling the function.
func someFunction(parameterWithDefault: Int = 12) {
// function body goes here
// if no arguments are passed to the function call,
// value of parameterWithDefault is 42
}
someFunction(6) // parameterWithDefault is 6
someFunction() // parameterWithDefault is 12
NOTE
Place parameters with default values at the end of a function ' s parameter list. This ensures-all calls to the function with the same order for their nondefault arguments, and makes it clear that the Same function is being called in each case.
Swift functions with external parameters