Using a multi-parameter function
We are not limited to just one parameter with our functions; we can also define multiple parameters. To create a multi-parameter function, we list the parameters in parentheses and separate the parameter definitions with commas. Let's look at how to define multiple parameters in a function:
func sayHello(name: String, greeting: String) {
print("\(greeting) \(name)")
}
In the preceding example, the function accepts two arguments: name
and greeting
. We then print a greeting to the console using both parameters.
Calling a multi-parameter function is a little different from calling a single-parameter function. When calling a multi-parameter function, we separate the parameters with commas. We also need to include the parameter name for all the parameters. The following example shows how to call a multi-parameter function:
sayHello(name:"Jon", greeting:"Bonjour")
We do not need to supply an argument...