Use External Parameter Names in Swift
Written by Team Kodeco
Swift functions can use external parameter names to give parameters more meaningful names when the function is called. The external parameter name is used when calling the function, while the internal parameter name is used within the function body.
To use external parameter names, you can write the external parameter name before the internal parameter name in the function definition, separated by a space.
For example, the following function calculates the area of a rectangle:
func calculateArea(of width: Int, and height: Int) -> Int {
return width * height
}
Here, of
and and
are the external parameter names, and width
and height
are the internal parameter names. To call this function, you would use the external parameter names:
let area = calculateArea(of: 5, and: 10) // 50
Alternatively, you can use _
as the external parameter name to indicate that the parameter should not be used when calling the function:
func calculateArea(_ width: Int, _ height: Int) -> Int {
return width * height
}
In this example, you can call the function like this:
let area = calculateArea(5, 10) // 50
Using external parameter names can make your code more readable and organized, allowing you to more clearly express the intent of the function.