-1

I am learning swift, in apple's tutorial, it has the following code snippet when introducing function:

func greet(name: String, lunch: String) -> String {
    return "Hello \(name), eat \(lunch)."
}
//Why here 'lunch' cannot be removed? 
greet("Bob", lunch: "Tuesday")

The code defined an greet(String, String) function, but when it calls the function:

greet("Bob", lunch: "Tuesday")

the 1st parameter doesn't have label 'name', but the 2nd one has label 'lunch', if I remove label 'lunch', compiler error says "Missing argument label lunch in call"

Why the 1st parameter could be without label, but not the 2nd one?

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
user842225
  • 5,445
  • 15
  • 69
  • 119
  • 1
    possible duplicate : http://stackoverflow.com/questions/24045890/why-does-a-function-call-require-the-parameter-name-in-swift – Victor Sigler Jul 14 '15 at 16:25
  • [Functions](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html) and scroll down to Function Parameter Names. – zrzka Jul 14 '15 at 16:27

2 Answers2

1

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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

Swift functions can specify local and external argument names:

func greet(who name: String = "world") {
    println("hello \(name)")
}

// prints "hello world"
greet()

// prints "hello jeff"
greet(who:"jeff")

// error
greet("jeff")

// error
greet(name: "jeff")

To opt out of this behavior you can use an underscore for the external name. Note that the first parameter implicitly uses the "no external name" behavior:

func greet(name: String = "world", _ hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hello world"
greet()

// prints "hello jeff"
greet("jeff")

// prints "hi jeff"
greet("jeff", "hi")

// error
greet(name: "jeff")

You can use the # prefix to use the same local and external name for the first parameter:

func greet(#name: String = "world", hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hi jeff"
greet(name: "jeff", hello: "hi")

Taken from Swift error: missing argument label 'name:' in call

Community
  • 1
  • 1
Constuntine
  • 498
  • 2
  • 16
  • Worth noting that the `#` syntax had been removed from Swift as of Swift 2.0. To accomplish this in Swift 2.0, you'd use `func greet(name name: String = …` – Aaron Brager Jul 14 '15 at 16:30