Swift supports parameter overloading, which allows for functions to have the same name and only be differentiated by the parameters that they take.
Let's learn more about parameter overloading by entering the following code into a playground:
func combine(_ givenName: String, _ familyName: String) -> String {
return "\(givenName) \(familyName)"
}
func combine(_ integer1: Int, _ integer2: Int) -> Int {
return integer1+integer2
}
let combinedString = combine("Finnley", "Moon")
let combinedInt = combine(5, 10)
print(combinedString) // Finnley Moon
print(combinedInt) // 15
Both the preceding functions have the name combine, but one takes two strings as parameters, and the other takes two Ints. Therefore, when we call the function, Swift knows which implementation we intended by the values we pass as parameters.
We've introduced something new in the preceding function declarations, anonymous parameter labels...