Function Overloading in Swift
In Swift, Function overloading allows for the creation of multiple functions with the same name, provided they have distinct function signatures. The function signature is determined by the number of parameters, the types of parameters, and the argument labels used. The return type alone is not sufficient to distinguish overloaded functions.
In Swift, two or more functions may have the same name if they different number of parameters, different types of parameters, or both. These functions are called overloaded functions and this feature is called function overloading.
Example:
// function with Int type parameter
func displayValue(value: Int) {
print("Integer value:", value)
}
// function with String type parameter
func displayValue(value: String) {
print("String value:", value)
}
// function call with String type parameter
displayValue(value: "Webeduclick")
// function call with Int type parameter
displayValue(value: 5)
Output:
String value: Webeduclick
Integer value: 5