Function parameters
In the previous example, the function simply prints out a message. That’s great, but sometimes you want to parameterize your function, which lets the function perform differently depending on the data passed into it via its parameters.
As an example, consider the following function:
func printMultipleOfFive(value: Int) { print("(value) 5 = (value 5)")
}
printMultipleOfFive(value: 10)
Here, you can see the definition of one parameter inside the parentheses after the function name, named value and of type Int. In any function, the parentheses contain what’s known as the parameter list. The parentheses are required to invoke the function, even if the parameter list is empty.
This function will print out any given multiple of five. In the example, you call the function with an argument of 10, so the function prints the following:
10 * 5 = 50
Note: Take care not to confuse the terms “parameter” and “argument”. A function declares its parameters in its parameter list. When you call a function, you provide values as arguments for the functions parameters.
You can take this one step further and make the function more general. With two parameters, the function can print out a multiple of any two values:
func printMultipleOf(multiplier: Int, andValue: Int) { print("(multiplier) (andValue) = (multiplier andValue)")
}
printMultipleOf(multiplier: 4, andValue: 2)
There are now two parameters inside the parentheses after the function name: one named multiplier and the other named andValue, both of type Int.
Notice that you need to apply the labels in the parameter list to the arguments when you call a function. In the example above you need to put multiplier: before the multiplier and andValue: before the value to be multiplied.
In Swift, you should try to make your function calls read like a sentence. In the example above, you would read the last line of code like this:
Print multiple of multiplier 4 and value 2
You can make this even clearer by giving a parameter a different external name. For example, you can change the name of the andValue parameter:
func printMultipleOf(multiplier: Int, and value: Int) { print("(multiplier) (value) = (multiplier value)")
}
printMultipleOf(multiplier: 4, and: 2)
You assign a different external name by writing it in front of the parameter name. In this example, the internal name of the parameter is now value while the external name (the argument label) in the function call is now and. You can read the new call as:
Print multiple of multiplier 4 and 2
If you want to have no external name at all, then you can employ the underscore _, as you’ve seen in previous chapters:
func printMultipleOf(_ multiplier: Int, and value: Int) { print("(multiplier) (value) = (multiplier value)")
}
printMultipleOf(4, and: 2)
This makes it even more readable. The function call now reads like so:
Print multiple of 4 and 2
You could, if you so wished, take this even further and use _ for all parameters, like so:
func printMultipleOf( multiplier: Int, value: Int) { print("(multiplier) (value) = (multiplier value)")
}
printMultipleOf(4, 2)
In this example, all parameters have no external name. But this illustrates how you use the underscore wisely. Here, your expression is still understandable, but more complex functions that take many parameters can become confusing and unwieldy with no external parameter names. Imagine if a function took five parameters!
You can also give default values to parameters:
func printMultipleOf( multiplier: Int, value: Int = 1) { print("(multiplier) (value) = (multiplier value)")
}
printMultipleOf(4)
The difference is the = 1 after the second parameter, which means that if no value is provided for the second parameter, it defaults to 1.
Therefore, this code prints the following:
4 * 1 = 4
It can be useful to have a default value when you expect a parameter to be one particular value the majority of the time, and it will simplify your code when you call the function.