In this article, we are going to learn about one of the important properties of object-oriented programming concepts known as function overloading. 

How Does Function Overloading Work?

Function Overloading follows a simple principle and makes sure that the function name remains the same, whereas the list of arguments and their data types varies on the programmer. Whenever a function is overloaded and is called in the main program, if the set and type of input actual parameters match the syntax of the formal arguments then the function execution will happen. If the exact match is not found then the promotion of data types takes place (standard type conversion) such as float converting into double and in the last case, it represents error.

Introduction to Function Overloading in C++

In C++, we have the concept of Object Oriented Programming (also known as OOPS). There are different properties in OOPS, out of which we will discuss function overloading. Every programming language provides the facility of reusing the piece of code which is written once using functions. Generally, depending on the declaration of the function the programmer decides the number of parameters and the type of parameters to be passed on to the function. Programming languages that support OOPS concepts have an additional feature concerning functions, often called function overloading. It refers to the fact that we can have several functions with the same name but with different parameters. 

What is Function Overloading in C++?   

Functional Overloading is a property where two or more functions can share the name but not the same list of arguments. It is one of the salient features of C++. Function overloading can also be treated as compile-time polymorphism. Upon a keen observation, the name remains the same whereas the order, data type, and entire list of arguments change. Let us look at an example of function overloading in C++.

Example:

#function1

void addPodium(int a, int b)

{

cout << a + b;

}

#function2

float addPodium(float a, float b, float c)

{

return (a + b + c);

}

In the above example, we can observe that there are two functions being defined as a piece of code where the name of functions remains same as “addPodium” but the return type and the list of input arguments and their data types have been changed.

Why is Function Overloading in C++ Used?

The OOPS concepts provide multiple advantages over conventional structured programming languages. Function overloading is considered to be compile-time polymorphism. This feature of OOPS concepts enables the programmer to develop a function with a name unchanged, but with different execution behavior thereby making the code more readable and reusable. 

Example 1: Overloading Using Different Types of Parameter

A function can be overloaded in different ways, one of them is by using different types of parameters. Let us understand the situation by having a look at the example:

#include <iostream>

using namespace std;

class printmethods {

   public:

      void print(int i) {

         cout << "integer value: " << i << endl;

      }

      void print(double f) {

         cout << "floating value: " << f << endl;

      }

      void print(char* c) {

         cout << "character value: " << c << endl;

      }

};

int main(void) {

   printmethods obj;

   obj.print(512);  

   obj.print(90.75);  

   obj.print("Podium"); 

   return 0;

}

Output: 

integer value: 512

floating value: 90.75

character value: Podium

The above program has three methods with the same name (print) but with different execution behavior. It takes in different types of input of different types and prints them.

Example 2: Overloading Using Different Number of Parameters

A function can be overloaded in two different ways: one of them is by using a different number of parameters. Let us understand the situation by having a look at the example:

#include <iostream>

using namespace std;

class printmethods {

   public:

      void add(int i, int j) {

         cout << "integer sum: " << i+j << endl;

      }

      void add(double f) {

         cout << "floating value: " << f << endl;

      }     

};

int main(void) {

   printmethods obj;

   obj.add(5,3);  

   obj.add(90.75);  

   return 0;

}

Output: 

integer sum: 8

floating value: 90.75

The above program has two methods with the same name (add) but with different execution behavior and different set of parameters. 

Rules of Function Overloading in C++

There are certain rules to be followed while overloading a function in C++. Let us have a look at some of them:

  1. The functions must have the same name
  2. The functions must have different types of parameters. 
  3. The functions must have a different set of parameters.
  4. The functions must have a different sequence of parameters. 

Function Overloading in C++ Example

Let us have a look at an example of Functions Overloading in C++.

#include <iostream>

using namespace std;

class add

 {

    public:

    int sum(int a,int b) 

    {

        return a+b;

    }

    float sum(float a,float b, float c) 

    {

       return a+b+c;

    }

 };

int main(void)

{

    add obj;

    cout<<obj.sum(2, 11)<<endl;

    cout<<obj.sum(2.1, 10.12, 7.9);

    return 0;

}

Output:

13

20.12

In the above example, there are two methods of name ‘sum’ different return types and different input parameters and their data types. The first function takes in two int values and returns their sum, and the second function takes in three float values and returns the sum.

Types of Function Overloading in C++

There are two different types of function overloading in C++. They are

  1. Compile time overloading: During the time of compilation, the functions are overloaded using different signatures. The return type of the function, number of parameters, and types of parameters are considered to be the signature of that particular function.
  2. Runtime overloading: During the time of run time, the functions are overloaded. During the run, time overloading the function is overloaded with a different number of parameters.

Causes of function overloading in C++

There are several causes of function overloading in C++. Some of them are mentioned here:

1. Type Conversion 

Let us look at an example of type conversion in function overloading in C++.

#include<iostream>

using namespace std;

void Value(float);

void Value(int);

void Value(float x)

{

    std::cout << "Value of x is : " <<x<< std::endl;

}

void Value(int y)

{

    std::cout << "Value of y is : " <<y<< std::endl;

}

int main()

{

Value(1.2);

Value(7);

return 0;

}

Output: 

main.cpp: In function ‘int main()’:

main.cpp:19:10: error: call of overloaded ‘Value(double)’ is ambiguous

   19 | Value(1.2);

      |          ^

main.cpp:8:6: note: candidate: ‘void Value(float)’

    8 | void Value(float x)

      |      ^~~~~

main.cpp:12:6: note: candidate: ‘void Value(int)’

   12 | void Value(int y)

      |      ^~~~~

We can see that the reference to the called function is ambiguous, we think that Value(1.2) will be called first and then Value(7). But C++ treated every float value as a double value. 

2. Function With Default Arguments

 Let us look at an example

#include<iostream>

using namespace std;

void Value(int);

void Value(int,int);

void Value(int x)

{

    std::cout << "Value of x is : " <<x<< std::endl;

}

void Value(int y,int z=8)

{

    std::cout << "Value of y is : " <<y<< std::endl;

    std::cout << "Value of z is : " <<z<< std::endl;

}

int main()

{

    Value(13);

    return 0;

}

Output: 

main.cpp: In function ‘int main()’:

main.cpp:17:9: error: call of overloaded ‘Value(int)’ is ambiguous

   17 | Value(13);

      |         ^

main.cpp:6:6: note: candidate: ‘void Value(int)’

    6 | void Value(int x)

      |      ^~~~~

main.cpp:10:6: note: candidate: ‘void Value(int, int)’

   10 | void Value(int y,int z=8)

      |      ^~~~~

We can see that the reference to the called function is ambiguous, it is because the function is called in two different ways. When the statement Value(13) is called, the compiler gets confused and sends an ambiguous message. 

3. Function With a Pass by Reference  

Let us look at an example.

#include <iostream>

using namespace std;

void Value(int);

void Value(int &);

void Value(int a)

{

    std::cout << "Value of a is : " <<a<< std::endl;

}

void Value(int &b)

{

    std::cout << "Value of b is : " <<b<< std::endl;

}

int main()

{

    int x=1;

    Value(x);

return 0;

}

Output:

main.cpp: In function ‘int main()’:

main.cpp:20:12: error: call of overloaded ‘Value(int&)’ is ambiguous

   20 |     Value(x);

      |            ^

main.cpp:7:6: note: candidate: ‘void Value(int)’

    7 | void Value(int a)

      |      ^~~~~

main.cpp:12:6: note: candidate: ‘void Value(int&)’

   12 | void Value(int &b)

      |    

We can see that the reference to the called function is ambiguous.

Function Overloading by Changing the Number of Arguments

A function can be overloaded in two different ways: one of them is by using a different number of parameters. Let us understand the situation by having a look at the example:

#include <iostream>

using namespace std;

class printmethods {

   public:

      void add(int i, int j) {

         cout << "integer sum: " << i+j << endl;

      }

      void add(double f) {

         cout << "floating value: " << f << endl;

      }  

};

int main(void) {

   printmethods obj;

   obj.add(5,3);  

   obj.add(90.75);  

   return 0;

}

Output: 

integer sum: 8

floating value: 90.75

The above program has two methods with the same name (add) but with different execution behavior and different set of parameters. 

Function Overloading by Different Types of Arguments

A function can be overloaded in different ways, one of them is by using different types of parameters. Let us understand the situation by having a look at the example:

#include <iostream>

using namespace std;

class printmethods {

   public:

      void print(int i) {

         cout << "integer value: " << i << endl;

      }

      void print(double f) {

         cout << "floating value: " << f << endl;

      }

      void print(char* c) {

         cout << "character value: " << c << endl;

      }

};

int main(void) {

   printmethods obj;

   obj.print(512);  

   obj.print(90.75);  

   obj.print("Podium"); 

   return 0;

}

Output: 

integer value: 512

floating value: 90.75

character value: Podium

The above program has three methods with the same name (print) but with different execution behavior. It takes in different types of input of different types and prints them.

Advantages of Function Overloading in C++

Here are some of the advantages of using function overloading in C++

  1. It saves memory usage and enables the reusability of code.
  2. It makes the program execute faster.
  3. With function overloading the programmer can develop functions of different nature but with the same name
  4. It is simpler and easier to understand the code.

Disadvantages of Function Overloading in C++

Here are some of the disadvantages of using function overloading in C++

  1. In function overloading, the main disadvantage is that the functions with different return types can’t be overloaded.
  2. In the case of a static function, the same parameters cannot be overloaded.

Difference Between Function Overloading and Operator Overloading

Here are some of the differences between function overloading and operator overloading in C++

Function Overloading

Operator Overloading

One can overload a function with the same name, but with different parameters.

One can overload operators such as +, -, /

When a function is overloaded, it has the same name but a different execution with different parameters.

When an operator is overloaded, the operation depends on the operands.

It allows the user to call in different ways

It allows the user to have an extended meaning, other than the predefined meaning.

Difference Between Function Overloading and Function Overriding

Here are some of the differences between function overloading and function overriding in C++

Function Overloading

Function Overriding

It enables the programmer to have two or more functions with the same name but with different arguments

In the case of method overriding, the two methods have the same name, and same parameters, but one method is present in the parent class and the other method in the child class.

They have the same scope.

They have different scopes.

Occurs during compile time

Occurs during run time

Function overloading can occur without inheritance. 

It occurs only when inheritance occurs.

Conclusion

We hope that this article helped you in getting a clear understanding of function overloading in C++. If you are looking to enhance your software development skills further, we would recommend you check Simplilearn’s Full Stack Developer - MERN Stack. This course is designed in collaboration with Caltech CTME and can help you hone the relevant skills and make you job-ready.

If you have any questions or doubts, feel free to post them in the comments below, our team will get back to you at the earliest.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449

Learn from Industry Experts with free Masterclasses

  • Key 2025 Software Development Trends- Learn How To Leverage them for your career

    Software Development

    Key 2025 Software Development Trends- Learn How To Leverage them for your career

    9th Dec, Monday9:30 PM IST
  • Must-Know Full Stack Java Dev Career Trends for 2024

    Software Development

    Must-Know Full Stack Java Dev Career Trends for 2024

    6th Aug, Tuesday9:00 PM IST
  • Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    Software Development

    Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    27th Jun, Thursday7:30 PM IST
prevNext