Introduction :
Function :
A function is a set of statements clubbed together to perform a specific task.
// Swift program to illustrate how we can print a string using a function
// function to print "W3Colleges"
func print()
{
print("W3Colleges")
}
// Calling print function
print()
Function overloading :
In Swift two functions can have the same name provided that they either differ in number of parameters or the type of parameters they contain. The functions are known as overloaded functions and this feature is known as function overloading. Swift provides one additional functionality using which we can overload a number of function by the label name also.
// Swift program to illustrate function overloading
// Function 1
func myFunction(parameter1:Int, parameter2: Int, parameter3: Float){
}
// Function 2
func myFunction(parameter1: Int, parameter2: Int, parameter3: Float, parameter4: Float ){
}
// Function 3
func myFunction(parameter1: Int, parameter2: Int, parameter3: Int){
}
All functions are overloaded is above case as first and second functions differ in number of parameters, first and third functions differ in type of parameters and second and third functions differ in number of parameters as well as type of parameters.
1. Overloaded functions having same type but different number of parameters :
Two functions having same data type of corresponding parameters but they differ in numbers are allowed to have the same name of the functions.
Example :
Implementation :
// Swift program to illustrate function overloading
// function 1 to calculate sum
func printSum(value1: Int, value2: Int) {
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print(sum)
}
// function 2 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int) {
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3
// Print the sum
print(sum)
}
// function 3 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int, value4: Int) {
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3 + value4
// Print the sum
print(sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5,value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5,value2: 15, value3: 25)
// Calling printSum() function to calculate sum
printSum(value1: 5,value2: 15, value3: 25, value4: 35)
2. Overloaded functions having different type but having same number of parameters :
Two functions having same number of parameters but they differ in data types of corresponding parameters are allowed to have the same name of the functions.
Example :
Implementation :
// Swift program to illustrate function overloading
// function 1 to calculate sum
func printSum(value1: Int, value2: Int) {
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print(sum)
}
// function 2 to calculate sum
func printSum(value1: Int, value2: Float) {
// Calculating sum of value1 and value2
let sum = Float(value1) + value2
// Print the sum
print(sum)
}
// function 3 to calculate sum
func printSum(value1: Float, value2: Float) {
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print(sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5,value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5,value2: 15.123)
// Calling printSum() function to calculate sum
printSum(value1: 5.123,value2: 15.123)
3. Overloaded functions having the different labels :
Two functions having same data type of corresponding parameters and also have the same numbers of parameters but they have different label names are allowed to have the same name of the functions.
Example :
Implementation :
// Swift program to illustrate function overloading
// function 1 to calculate sum
func printSum(value1: Int, value2: Int) {
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print(sum)
}
// function 2 to calculate sum
func printSum(variable1: Int, variable2:Int) {
// Calculating sum of value1 and value2
let sum = variable1 + variable2
// Print the sum
print(sum)
}
// function 3 to calculate sum
func printSum(Number1: Int, Number2: Int) {
// Calculating sum of value1 and value2
let sum = Number1 + Number2
// Print the sum
print(sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 15, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 30, value2: 30)
// Calling printSum() function to calculate sum
printSum(value1: 60, value2: 60)