Swift – Function Overloading


Swift – Function Overloading



”;


A Function is a snippet of code that is used to perform a specific task. In Swift, we are allowed to overload functions. Function overloading is a technique in which we can create multiple same-name functions but with different parameters or argument labels.

So when an overloaded function is called, the compiler determines which function should execute according to the number of parameters and types of parameters provided at the time of call. In Swift, we cannot overload functions according to the return type.

We can overload functions in the following ways −

  • Overloading with a different number of parameters.

  • Overloading with different parameter types.

  • Overloading with argument label.

Overloading with Different Number of Parameters

In function overloading with a different number of parameters, the names of the two or more functions are the same but the number of parameters is different. The functions can have the same or different names of parameters. In this type of function overloading, the function is invoked according to the number of parameters provided by the user during the function call.

Example

Swift program to demonstrate function overloading with a different number of parameters.


import Foundation

// Function 1 
func product(num1: Int, num2: Int){

   // Calculating product 
   let result = num1 * num2
   print("Function 1: Product = ", result)
}

// Function 2  
func product(num1: Int, num2: Int, num3: Int){

   // Calculating product 
   let result = num1 * num2 * num3
   print("Function 2: Product = ", result)
}

// Function 3 
func product(num1: Int, num2: Int, num3: Int, num4: Int){

   // Calculating product 
   let result = num1 * num2 * num3 * num4
   print("Function 3: Product = ", result)
}

// Calling function 1
product(num1: 23, num2: 34)

// Calling function 2
product(num1: 53, num2: 34, num3: 34)

// Calling function 3
product(num1: 23, num2: 34, num3: 55, num4: 21)

Output

It will produce the following output −


Function 1: Product =  782
Function 2: Product =  61268
Function 3: Product =  903210

Overloading with Different Parameter Types

A function can be overloaded with different parameter types which means multiple functions contain the same name and number of parameters but the parameter types are different. In this type of function overloading, the function is invoked according to the type of parameters provided by the user during the function call.

Example

Swift program to add two strings using nested function.


import Foundation

// Function 1 
func Addition(num1: Int, num2: Int){

   // Adding two values
   let result = num1 + num2
   print("Function 1: Result = ", result)
}

// Function 2  
func Addition(num1: String, num2: String){

   // Adding two values
   let result = num1 + num2 
   print("Function 2: Result = ", result)
}

// Calling function 1
Addition(num1: 23, num2: 34)

// Calling function 2
Addition(num1: "Hello!", num2: "Tutorialspoint")

Output

It will produce the following output −


Function 1: Result =  57
Function 2: Result =  Hello!Tutorialspoint

Overloading with Argument Label

We can overload functions according to their argument labels. In such type of overloading, the function names are the same but the argument label is different. So that at the time of execution compiler can easily identify which function should it invoke.

Example

Swift program to overload function according to their argument labels.


import Foundation

// Function 1 
func Area(length: Int, width: Int){
   let result = length * width 
   print("Function 1: Result=", result)
}

// Function 2
func Area(l: Int, w: Int){
   let result = l * w
   print("Function 2: Result=", result)
}

// Calling function 1
Area(length: 23, width: 3)

// Calling function 2
Area(l:13, w: 3)

Output

It will produce the following output −


Function 1: Result= 69
Function 2: Result= 39

Advantages of Function Overloading in Swift

The following are the advantages of function overloading −

  • It improves the code readability.

  • Reduce multiple function names by giving the same name to all the functions.

  • It enhances type safety and reduces runtime errors.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *