Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – Function Overloading

  • Last Updated : 20 Dec, 2021

In Swift, a function is a set of statements clubbed together to perform a specific task. It is declared using the “func” keyword. We can also set the return type of the function by specifying the data type followed by a return arrow (“->”).

Syntax:

func functionName(parameters) -> returnType {

             // body

}

Example:

In the below program, we are printing a string by defining a function that neither contains any parameter nor return type.

Swift




// Swift program to illustrate how we
// can print a string using a function
  
// Function to print a string
// This function have no return type
func print()
{
     print("GeeksforGeeks"
}
  
// Calling print function
print()

Output:

GeeksforGeeks

Function Overloading

Function overloading is the most commonly used feature by developers. This technique allows them to create functions that share some common features like the same name, etc. In Swift, a pair of functions can have the same name provided that they are either different in the number of parameters, the type of at least one corresponding parameter, or the label of at least one corresponding parameter. These functions are known as overloaded functions and this feature is known as function overloading. It provides more flexibility and is useful when we require the same or similar result or functionality.

There are three methods using which we can achieve function overloading in Swift and they are discussed below:

Function Overloading with Different Number of Parameters

Functions that have the same data type of corresponding parameters but are different in the number of parameters they contain are allowed to have the same name of the functions. In the below program, we have three functions of the same name that is printSum but with a different number of parameters. Here, the first function contains two parameters of integer type, the second function contains three parameters of integer type whereas the third fun

n contains four parameters of integer.

Example:

Swift




// 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("The sum of", value1, "and",
           value2, "is equal to", 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("The sum of", value1, ","
           value2, "and", value3, 
           "is equal to", 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("The sum of", value1, ","
           value2, ",", value3, "and"
           value4, "is equal to", 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)

Output:

The sum of 5 and 15 is equal to 20
The sum of 5 , 15 and 25 is equal to 45
The sum of 5 , 15 , 25 and 35 is equal to 80

Function Overloading With the Same Number of Parameters

Functions having the same number of parameters but at least one corresponding parameters are different in data types are allowed to have the same name of the functions. In the below program, we have three functions of the same name that is printSum, although all the functions contain the same number of parameters but have different types. The first function contains two integer type parameters, the second function contains one integer and one float type parameter, and the third function contains two float type parameters.

Example:

Swift




// 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("The sum of", value1, "and"
           value2, "is equal to", 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("The sum of", value1, "and"
           value2, "is equal to", 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("The sum of", value1, "and"
           value2, "is equal to", 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)

Output:

The sum of 5 and 15 is equal to 20
The sum of 5 and 15.123 is equal to 20.123001
The sum of 5.123 and 15.123 is equal to 20.246

Function Overloading With Different Argument Labels

Two functions having the 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. In the below program, we have three functions of the same name that is printSum, although all the functions contain the same number and type of parameters but have different labels. The first function contains two integer type parameters with value1 and value2 labels, the second function contains two integer type parameters with variable1 and variable2 labels, and the third function contains two integer type parameters with number1 and number2 labels.

Example:

Swift




// 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("The sum of", value1, "and",
          value2, "is equal to", 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("The sum of", variable1, "and",
          variable2, "is equal to", 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("The sum of", number1, "and",
          number2, "is equal to", sum)
      
}
  
// Driver code
// 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)

Output:

The sum of 15 and 15 is equal to 30
The sum of 30 and 30 is equal to 60
The sum of 60 and 60 is equal to 120

Overloading Functions According to Return Type 

Can we overload functions on the basis of the return type of function only? The answer is No. We cannot overload functions by using different return types for the functions. In other words, if there are at least two functions in a program having the same data type of corresponding parameters and having exactly the same number of parameters then the compiler produces a compile-time error. In the below program the first function has an integer return type and contains two parameters of integer type, the second function has a double return type and contains two parameters of integer type. Moreover, corresponding parameters have the same label. Hence, this is not allowed in Swift. 

Example:

Swift




// Swift program to illustrate function overloading
  
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int) -> Int
{
  
    // Calculating sum of value1 and value2
    let sum = value1 + value2
      
    // Print the sum
    return(sum)
}
  
// Function 2 to calculate sum
func printSum(value1: Int, value2:Int) -> Double 
{
  
    // Calculating sum of value1 and value2
    let sum = value1 + value1
      
    // Print the sum
    return (Double)(sum)
}
  
// Driver code
// Calling printSum() function to calculate sum 
print("The sum of 15 and 25 is equal to"
      printSum(value1: 15, value2: 25))
  
// Calling printSum() function to calculate sum 
print("The sum of 25 and 35 is equal to"
      printSum(value1: 25, value2: 35))

Output:

main.swift:28:7: error: ambiguous use of 'printSum(value1:value2:)'
      printSum(value1: 15, value2: 25))
      ^
main.swift:4:6: note: found this candidate
func printSum(value1: Int, value2: Int) -> Int
     ^
main.swift:15:6: note: found this candidate
func printSum(value1: Int, value2:Int) -> Double 
     ^
main.swift:32:7: error: ambiguous use of 'printSum(value1:value2:)'
      printSum(value1: 25, value2: 35))
      ^
main.swift:4:6: note: found this candidate
func printSum(value1: Int, value2: Int) -> Int
     ^
main.swift:15:6: note: found this candidate
func printSum(value1: Int, value2:Int) -> Double 
     ^

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!