Categories: C++

Function Overloading in C++

What is Function Overloading & why is it used?

Function overloading is an object-oriented programming feature in which two or more functions have the same name but different parameters.
The function overloading feature in C++ is used to improve code readability. It is used to save the programmer from having to remember multiple function names. Function overloading can also be treated as compile-time polymorphism.

It should be noted that function declarations that differ only in return type cannot be overloaded.

Various Methods for Overloading a Function:
  • By using various types of arguments.
  • By varying the number of Arguments.
Function Overloading: Using various types of arguments
#include <iostream>
using namespace std;


void add(int x, int y)//sum of integers
{
cout << "sum = " << (x + y);
}

void add(double x, double y)//sum of double elements
{
 cout << endl << "sum = " << (x + y);
}


int main()
{
 add(10, 2);
 add(5.3, 6.2);

 return 0;
}
Output:
sum = 12
sum = 11.5

In the above example, we can see there are two add functions but with different types of arguments, i.e, add function is overloaded, due to which the compiler can easily differentiate which one of the functions is to be executed on the function call.

Function Overloading: varying the number of Arguments
#include <iostream>
using namespace std;


void add(int x, int y)//sum of two integers
{
cout << "sum = " << (x + y);
}

void add(int x, int y, int z)//sum of three integer elements
{
 cout << endl << "sum = " << (x + y + z);
}


int main()
{
 add(10, 2);
 add(5, 6, 10);

 return 0;
}
Output:
sum = 12
sum = 21

Here it is clear that add function is overloaded by changing the number of arguments, although keeping the data type same.

Default Arguments in function:

When we specify a default value for a parameter while declaring a function, we refer to it as the default argument. In this case, even if we call the function without specifying a value for that parameter, the function will use the default value. For instance,

#include <iostream>
using namespace std;


void add(int x, int y=9)//sum of two integers
{
cout << "sum = " << (x + y);
}


int main()
{
 add(10);
 add(10, 0);
 add(5, 6);

 return 0;
}
Output:
sum = 19
sum = 10
sum = 11
Important points to be Noted:
  • Only the last argument must be left blank.
  • If you default one argument, you must default all subsequent arguments as well.
  • You can give any value to the argument as a default value that is compatible with its datatype.
  • The difference between default arguments and constant arguments is that constant arguments cannot be changed, whereas default arguments can be overwritten if necessary.

Note: also read about Inline Function in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago