Function Overloading

Function Overloading


What is Function Overloading?

Function overloading is a feature where two or more functions can have the same name if the number and/or type of arguments passed is different.

These functions having the same name but different arguments are known as overloaded functions.

Example :

//same name different arguments

int eruditors(int x) { }
float eruditors (float x) { }
int eruditors (int x, int y) { }

Types of Function Overloading :

There are two types of functions in C++  :-

1. Function Overloading Using Different Types of Parameters :
Program :

#include <iostream>
using namespace std;

// function with int type parameter
int overLoad(int x)
{
    cout << "Overloaded Int Value : " << x << endl;
}

// function with float type parameter
float overLoad(float y)
{
    cout << "Overloaded Float Value : " << y << endl;
}

int main() 
{
    int a = 56;
    float b = 11.7;
    
    // calling 2 functions with different types parameter
    overLoad(a);
    overLoad(b);

    return 0;
}


Output :



2. Function Overloading Using Different Number of Parameters :

#include <iostream>
using namespace std;

// function with two parameters
int overLoad(int x)
{
    cout << "Overloaded One Value : " << x << endl;
}

// function with one parameter
int overLoad(int x, int y)
{
    cout << "Overloaded Two Values : " << x << " " << y << endl;
}

int main() 
{
    int a = 56, b = 117;
    
    // calling 2 functions with different numbers of parameters
    overLoad(a);
    overLoad(a,b);

    return 0;
}


Output :