×
BLUE
C++: Function overloading


C++: Function overloading
  1 June 2021
Read more Like this Post.
Function overloading mean the multiple function with same name but different parameters. The function parameters should be different or number of parameters is different. After this difference compiler decide to call which function.

output of the code:
calling with empty parameters calling with integer: 23

there is two function in above example function name is _fun_ first function have no parameters and second function have one parameters name ( x ) in main function call the function first calling function is empty parameters and second have a integer argument. First function only print a message and second print the giving value. When its call the first function he searching a function with have no arguments and invoked the function in second line when its call this function again and give a parameters on it its search a function with a integer parameters and invoked the function which accept the integers. This process calls function overloading. Function overloading also work in different data-type in parameters like.

Example:

#include 
using namespace std;

// section 1
int _function_(){
    cout << "function call with no parameters" << endl;
    return 0;
}
int _function_(int x){
    cout << "function called with integer data type: " << x<< endl;
    return 0;
}
double _function_(double x){
    cout << "function called with double data type: " << x<< endl;
    return 0;
}
float _function_(float x){
    cout << "function called with floating point data type: " << x<< endl;
    return 0;
}

int main(){
    // section 2
    _function_();
    _function_(2.03);
    _function_(21);
    _function_(11.512);
}

output of the code:
The output of the code is: function call with no parameters function called with double data type: 2.03 function called with integer data type: 21 function called with double data type: 11.512

In the above example code contain multiple function with same name but different parameters like first function is empty with no parameter the second function is accept only integer type parameter and the 3rd function accept only double’s the 4th function accept floating point data. When its call every function called by his data-type. More example’s below.

Example:

#include 
sumOf(){
    std::cout << "Enter something to add" << std::endl;
}
float sumOf(float x, float y){
    std::cout << "add floats: " << x   y << std::endl;
}
int sumOf(int x, int y){
    std::cout << "add integers: " << x   y << std::endl;
}
double sumOf(double x, double y){
    std::cout << "add doubles: " << x   y << std::endl;
}
int main(){
    sumOf(12.101, 91.201);
}

Output of the code is:
add doubles: 103.302

the above example have function to add some values. Now we can add different type of value together without any tension. Only calling the function and put the values on it.
Quiz: Make a program that take values from user and add together.

Passing different type of Arguments and return type

In function overloading let pass different type of parameters and return type.

Example:

#include 
using namespace std;
// function - 1
float _fun_(int x, int y){
    return x   y;
}
// function - 2
float _fun_(int x, double y){
    return x   y;
}
// function - 3
int _fun_(double x, float y){
    return x   y;
}
// function - 4
double _fun_(double x, int y){
    return x   y;
}

int main(){
    cout << _fun_(6, 23) << endl;
    cout << _fun_(20, 20.3) << endl;
    cout << _fun_(16, 10.103) << endl;
    cout << _fun_(11.06, 40) << endl;
}

output:
29
40.03
26.103
51.06

In the example code have function with different return type and arguments. Function take two integer but return is float, second function return type is float and take integer and double in parameter, third function return type is int and take double and float third function return type is double take double and int parameters and main function is calling the all function by different data-types first function take integer and integer 2nd integer and double 3rd is double float and last have double integer.

Advantage of function overloading

  • it will be faster The speed of the program.
  • Function overloading save the memory .
  • In code made changes will be easy.
  • We can set default operation of a function.
Related Post's

cpp: function   cpp: string   cpp: function overloading   string  
Not Loaded Zohaib Jozvi
0 1 June 2021
Read Latest Post.
Read other post.