Clarity
The main reason is clarity.
In Swift's function naming style, the method name usually implies something about the first argument, but not later arguments.
For example:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething", name: UIScreenBrightnessDidChangeNotification, object: nil)
self
is implied by the syntax to be the observer you're adding to the notification center.
greet("Bob", "Tuesday")
would imply that Bob is being greeted, but doesn't tell you anything about what Tuesday has to do with things. This makes code less readable, so it isn't allowed.
Default Parameter Values
A second reason is default parameter values. Consider instead this function:
func greetForLunch(name: String, food: String = "pasta", restaurant: String = "Olive Garden") -> String {
return "Hello \(name). Would you like to eat \(food) at \(restaurant)?"
}
Since the two latter arguments have default values, you can omit one or both of them, for example:
greetForLunch("Bob", food: "endless breadsticks")
// "Hello Bob. Would you like to eat endless breadsticks at Olive Garden?"
But if you didn't specify food
here, like so:
greetForLunch("Bob", "endless breadsticks")
It would be ambiguous about whether the second argument should be assigned to food
or restaurant
.