C++ Tutorial – Function overloading

Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.

For example in this C++ Tutorial let us assume an AddAndDisplay function with different types of parameters.

//C++ Tutorial - Sample code for function overloading
void AddAndDisplay(int x, int y)
{
cout<<" C++ Tutorial - Integer result: "<<(x+y);
}

void AddAndDisplay(double x, double y)
{
cout<< " C++ Tutorial - Double result: "<<(x+y);
}

void AddAndDisplay(float x, float y)
{
cout<< " C++ Tutorial - float result: "<<(x+y);

}

Some times when these overloaded functions are called, they might cause ambiguity errors. This is because the compiler may not be able to decide what signature function should be called.

If the data is type cast properly, then these errors will be resolved easily. Typically, function overloading is used wherever a different type of data is to be dealt with. For example this can be used for a function which converts farenheit to celsius and vice versa. One of the functions can deal with the integer data, other can deal float for precision etc.,