Function Overloading in C++

The principle of function overloading helps to implement polymorphism in C++ in which two or more functions share the same name (overloaded functions) and perform a general action but using different methods or approaches. The overloaded functions must differ in their function/method parameters/signatures and are defined and coded separately. They may or may not differ in the return types. Thus by the concept of function overloading, the programmer could make use of polymorphism in which he could use the same function name for performing the general course of actions in different circumstances.

Below is a simple C++ program showing overloading of the abs() function.

#include <iostream>

using namespace std;

int abs(int i);
double abs(double d);
long abs(long l);

        cout << abs(-20) << "\n";
        cout << abs(+20.2) << "\n";
        cout << abs(-20000.345453454) << "\n";
        return 0;

int abs(int i){
        return i<0? -i : i;

double abs(double f){
        return f<0.0? -f : f;

long abs(long l){
        return l<0? -l : l;


As shown above, with the single function name ‘abs’, the programmer could deal with different number types. The compiler will select the appropriate function from the type of the function parameter.