What is function overloading? Discuss this.

Last updated on June 13th, 2020 at 08:34 pm

Function Overloading: Overloading refers to the use of the same thing for different purposes. When the same name for different functions is used that is called function overloading. The functions are then distinguished by their parameters or argument lists.
Functions can be overloaded by changing, increasing or decreasing their arguments,

float add(float a, float b)
int add(int a, int b)
int add(int a)
int add(int a, int b, int c)

By changing their argument’s sequence like,

double add(int a, double b)
double add(double a, int b)

Following is a program example using function overloading,

//function overloading
#include<iostreams.h>

class overload{
int a,b;
float c,d;
public:
int add(int x, int y){ //function 1
return x*x+y*y;
}
float add(float x,float y){ //function 2
return (x+x)*(y+y);
}
};

void main(){
overload ob1;
cout<<ob1.add(5,5)<<"n"; //calling function 1 here
cout<<ob1.add(5.5f,5.5f); //calling function 2 here
}


Output:
50
121

Here both function 1 and 2 are of the same name add but their arguments are different. Thus they are treated as two different functions.

Leave a Reply

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