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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago