Function overloading c++
Function overloading
In this tutorial, you will understand the concept of function overloading in the c++ language and what are the advantages of function overloading.
How to overload function in c++?
Functions overload with the variation of arguments and data types of arguments with the same name of the function. The name of the function will be the same in all functions but different data types and numbers of arguments. The prototype of function decides the function's call during calling the function.
First look at the function overloading with the different number of parameters in the parameter list.
The parameter list :
return data type function name(parameter 1,parameter 2,...)
These parameters are also known as arguments.
Function overload on the base of the numbers of parameters.
Syntax :
Function name fun.
int fun(int a)
int fun(int a,int b)
int fun(int a,int b,int c)
Example:
#include<iostream>
using namespace std;
void fun(int a);
void fun(int a,int b);
void fun(int a,int b,int c);
void fun(int a)
{
cout<<" number "<<a<<endl;
}
void fun(int a,int b)
{
cout<<" numbers "<<a<<" "<<b<<endl;
}
void fun(int a,int b,int c){
cout<<" numbers "<<a<<" "<<b<<" "<<c<<endl;
}
int main()
{
int num1=1,num2=2,num3=3;
fun(num1);
fun(num1,num2);
fun(num1,num2,num3);
return 0;
}
Output
number 1
numbers 1 2
numbers 1 2 3
In the above example, three functions with the same name fun but numbers of parameters are different.
At the time of calling function the number of the argument passed in the function help the compiler to call that function.
We can call the same function with a different arguments.
Function overloading based on the data type of arguments
In this type of function overloading function will be the same but data types of arguments of parameter list are different.
syntax :
Function name fun.
int fun(int a,int b)
int fun(float a,float b)
Example:
#include<iostream>
using namespace std;
int sum(int a,int b);
double sum(float a,float b);
int sum(int a,int b)
{
int sum=a+b;
return sum;
}
double sum(float a,float b)
{
double sum=a+b;
return sum;
}
int main()
{
int num1=1,num2=2;
float n1=1.4,n2=2.4;
//call the function with integer argument
cout<< sum(num1,num2)<<endl;
//call the function with float argument
cout<<sum(n1,n2)<<endl;
return 0;
}
Output:
3
3.8
Function overloading can not done on the based of the return data type of function. If two or more functions have same name and parameters but different return data type of functions then compiler show error.for example
int sum(int a, int b)
float sum(int a,int b)
double(int a, int b)
Compiler show error, actually function overloading is the run time polymorphism that why compiler show error.
During the function, overloading takes care of the return data type.
What are the advantages of function overloading?
Function overloading increases the reusability and readability of code. Think about that we make the function of the sum which takes 2 arguments but the user wants to sum three numbers.
There is function overloading helps us to make the same name of a function with different parameters. If the user also wants different data type for summation then again function overloading perfectly help us , This is one example we take but really function overloading helpful in the reusability of code and to make the program user-friendly program.
Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.
Nice post thanks for this valuable information
ReplyDelete