Strictly speaking, you don’t need them when writing your own functions. But it’s a bit of sugar that make Swift code easier to read and understand. It helps to make your code more readable and, therefore, more maintainable.
To quote the official Swift docs: “The use of argument labels can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that is readable and clear in intent.”
And the example used in the docs:
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill! Glad you could visit from Cupertino."
Here both the function body and the call site read smoothly and the intent is well established.
Without argument labels, your call site would be something like this:
which doesn’t read as nicely. But if you just used the argument label without a separate parameter name, then your function would be something like this:
func greet(person: String, from: String) -> String {
return "Hello \(person)! Glad you could visit from \(from)."
}