Omitting argument labels
All the functions in this chapter have used labels when passing arguments into the functions. If we do not want to use labels, we can omit them by using an underscore. The following example illustrates this:
func sayHello(_ name: String, greeting: String) {
print("\(greeting) \(name)")
}
Notice the underscore prior to the name
label in the parameter list. This indicates that the name
label should not be used when calling this function. Now, we are able to call this function without using the name
label:
sayHello("Jon", greeting: "Hi")
This call would print out Hi Jon
.
Now, let's put what we have covered together and see a more complex example.