-6

I don't understand the way Apple explain. What does it mean ":" between parameters?

It is used for assigning a function to a variable like this

func sum(a: Int, b: Int) -> Int {
    return(a+b)
}

let addTwoNumbers = sum(a:b:)
addTwoNumbers(1, 2)
//prints 3
rmaddy
  • 314,917
  • 42
  • 532
  • 579
John
  • 675
  • 1
  • 7
  • 16
  • 1
    have you check this [link](https://developer.apple.com/documentation/swift/1641736-print) – Pratik Prajapati Apr 29 '19 at 13:32
  • 1
    If you are refereing to the syntax it denotes a parameter "_:", "separator:", "terminator:" so 3 parameters, first one is anonymous the other two are named "separator" and "terminator". The reason you don't have to use the last two in your example is that they have default values. – Joakim Danielson Apr 29 '19 at 13:36
  • See this [link](https://stackoverflow.com/a/30865283/11327526) – Darshan Kunjadiya Apr 29 '19 at 13:43
  • Please read the Functions chapter of the Swift book, specifically the [Function Argument Labels and Parameter Names](https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID166) section. – rmaddy Apr 29 '19 at 16:28
  • I got the Answer below and understood. Thank you! – John Apr 29 '19 at 16:29

1 Answers1

1

There are 2 overloads of print (2 different functions with the same name) - this one and this one.

If you just say print, it is ambiguous which overload you mean. Therefore, you also specify the parameter labels of the functions, so the first overload is called print(_:separator:terminator:) and the second is called print(_:separator:terminator:to:).

Let's dissect print(_:separator:terminator:). We can see that it has three parameter labels - _, separator and terminator. The : are just there to separate the labels. It is also the character you write after the label when you call the function:

print("hello", "world", separator: " ")
                                 ^

so it kind of makes sense.

Sweeper
  • 213,210
  • 22
  • 193
  • 313