C++ Function Overloading

This tutorial explains the concept of C++ function overloading and how it is used in programs.

C++ programming function overloading

As we know that functions are the piece of code that can be used anywhere in the program with just calling it multiple times to reduce the complexity of the code. In POP, we can use as many functions as per need, however, the names of the function shouldn’t match. In the case of OOP, we can use a function name as many times following the condition that the number of arguments or the type of arguments must differ.

So the method of using the same function name for different functions is simply called function overloading. When an overloaded function is called from main program the function with matching arguments is invoked.


void display();              //function with no arguments
void display( int );         //function with one integer type arguments
void display( float );       //function with one floating point arguments
void display( int, float );  //function with one floating and one integer type arguments

Illustration of C++ function overloading C++ Function Overloading

Example to illustrate the concept of C++ function overloading

// functionoverloading.cpp
 #include <iostream>
 using namespace std;
 void display ( ) //function with no arguments
  int a = 3;
  cout << a << endl;
void display (int a ) //function with one integer argument
  cout << a << endl;
void display (double a ) //function with one floating argument
   cout << a << endl;
void display(int a, float b) //function with one integer and one floating arguments
   cout<< a << " , " << b << endl;

int main()
  display(); //function call with no arguments
  display(5); //function call with one integer argument
  display(2.3); //function call with one floating argument
  display(5,4.0); //function call with one integer and one floating arguments
  return 0;
 } //end of program


5 , 4

In above program, when we call the function with no arguments the display()function with no arguments is invoked and the result is 3. Similarly, when we pass one integer type argument while calling the display(int a) function is invoked. Similarly, when we pass integer and float value display(int a, float b) function is invoked. The advantage of function overloading is that the number of names of functions is reduced.

So function overloading is achieved by varying following points:

  • the number of parameters
  • datatype of the parameters
  • the order of the appearance of parameters
Common Programming Error
While defining overloaded function with identical parameter lists but different return type results in a compilation error.