Home > OOP > Function Overloading in C++

Function overloading means that more than one functions of the same name within the same scope. C++ allows us to overload a function as many times as we want or need. It allows the programmer to provide different semantics for a function according to its needs.

Function Overloading in c++

We can do Function Overloading in C++ by:

  1. Different types of Arguments.
  2. Different Number of Arguments.

You can confirm function overloading by looking at these two parameters in the different definitions of the function.

Different Types of Arguments in the argument list

What do we mean by different type of arguments? Different type of arguments refers to the different data types of arguments in the argument list. I.e. Data type of at least one argument in this definition must differ from the previous definition.
E.g.

void my_Function (int a, int b)
{
    // function Body
}
void my_Function (float a, float b)
{
    // function Body
}
void my_Function (float a, char b)
{
    // function Body
}

The above three definitions of the function differ from each other as in each definition the data type for arguments is different from the previous definition. This is function overloading in c++ by different types of arguments.

Different Number of Arguments in the argument list

A different number of arguments refers to the different total number of arguments in the argument list. I.e. The total arguments in the argument list of this definition of the function must differ from the previous definition.

E.g.

void my_Function ()
{
   // function Body
}
void my_Function (float a)
{
   // function Body
}
void my_Function (int a, int b)
{
   // function Body
}

The above three definitions of the function differ from each other as in each definition the total number of arguments differs from the previous definition. This is function overloading in c++ by the different number of arguments.

 

Example program: –

#include "stdafx.h"
#include "iostream"

using namespace std;

void sum(int, int);
void sum(float, float);
void sum(int, int, int);

int main()
{
     int no1 = 10, no2 = 20, no3 = 30;
     float n1 = 15.6, n2 = 13.8;
     sum(no1, no2);
     sum(n1, n2);
     sum(no1, no2, no3);
     return 0;
}
void sum(int x, int y)
{
     cout << "Sum of Two Integer Values is : " << x+y<<endl;
}
void sum(float a, float b)
{
     cout << "Sum of Float Values is : " << a + b<<endl;
}
void sum(int u, int v, int w)
{
     cout << "Sum of Three Integer Values is : " << u + v + w<<endl;
}

Output: –
Function Overloading in c++

When are functions not Overloaded?

The most important thing to remember is that different return types for each definition of the function will not overload a function. I.e. if we only change the return type for a function than the function is not overloaded.
E.g.


void my_Function ()
{
    // function Body
}
int my_Function ()
{
    // function Body
}
float my_Function ()
{
    // function Body
}

The above three definitions of the function will not overload the function as the change in the only return type of the function will not overload it.

One thought on “Function Overloading in C++

Leave a Reply

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