1

I have the following piece of code written in swift:

func hai(greeting: String, times: Int) -> String {
    return "You are greeted + \(greeting) + \(times) times "
}
hai ("Hello", times: 3)

When I call the function hai, if I call it the following way hai("hello", 3) it throws an error and forces me to call the way it was mentioned above.

Can someone please explain why this should be the case? Thanks, I am new to IOS programming.

djromero
  • 19,551
  • 4
  • 71
  • 68
Srikanth
  • 67
  • 9

1 Answers1

3

Each function parameter in Swift has two names - an internal and an external one. When you define function signature the way you did, the external parameter name of times is the same as its internal name. You can tell Swift that you do not want an external name by placing _ in the external name position:

func hai(greeting: String, _ times: Int) -> String
//                         ^

Learn more about internal/external parameter names in the Swift 2.2 Programming Language Guide.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523