Open In App

Function Overloading in C++

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ function overloading allows you to define multiple functions with the same name but different parameters. It is a form of compile time polymorphism in which a function can perform different jobs based on the different parameters passed to it. It is a feature of object-oriented programming that increases the readability of the program.

Example

Assume that you have to add 2 integers. It's easy to make a function for that as shown:

C++
int add2(int a, int b) {
    return a + b;
}

Now, assume that you also need a function that adds 3 integers. One method is to create another function with different name:

C++
int add3(int a, int b, int c) {
    return a + b + c;
}

But in this case, it may be difficult for you to understand the behaviour of the function because its name differs. Moreover, it is very possible to forget about the other function.

This is where function overloading comes into play. It allows us to create both of the functions with same name as shown:

C++
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

// Same function with different arguments
int add(int a, int b, int c) {
    return a + b + c;
}

int main() {
    int a = 5, b =7, c = 11;

    // Add function to add two numbers
    cout << add(a, b) << endl;

    // Add function to add three numbers
    cout << add(a, b, c);

    return 0;
}

Output
12
23

Different Ways of Function Overloading

A function in C++ can be overloaded in three different ways:

  • By having different number of parameters.
  • By having different types of parameters.
  • By having both different number and types of parameters.

Different Number of Parameters

We can overload a function by changing the number of parameters that it accepts.

Example: This program demonstrates function overloading with different number of parameters. The given program has two different function definitions for the function fun with different number of parameters.

C++
#include <iostream>
using namespace std;

// Two functions taking different number
// of parameters
int multiply(int a, int b);
int multiply(int a, int b, int c);

int main() {
    
    // Calling multiply function with
    // different number of arguments
    cout << multiply(10, 2) << endl;
    cout << multiply(5, 6, 4);

    return 0;
}

// Definition of functions
int multiply(int a, int b) {
    return a * b;
}
int multiply(int a, int b, int c) {
    return a * b * c;
}

Output
20120

Different Types of Parameters

We can also change the data type of the parameters while keeping the same function name in function overloading.

Example: This program demonstrates function overloading with different data types of parameters. The given program has two different function definitions for the function fun with parameters of different data types (double and int) .

C++
#include <iostream>
using namespace std;

// Function with different arguments type
int add(int a, int b);
double add(double a, double b);

int main() {
    
    // Calling add function with different
    // argument type
    cout << add(10, 2) << endl;
    cout << add(5.3, 6.2);

    return 0;
}

// Function definitions
int add(int a, int b) {
    return a + b;
}
double add (double a, double b) {
    return a + b;
}

Output
12
11.5

Different Number and Types of Parameters

Functions can also be overloaded by changing both the number and types of parameters.

Example: In the given program the function fun is overloaded and has two different function definitions with different order of parameters of different data types.

C++
#include<iostream>
using namespace std;

// Functions overloaded with different
// argument number and types
int add(int a, double b);
double add(double a, int b, int c);

int main() {
    
    // Calling both overloaded versions
    cout << add(10, 2.5) << endl;
    cout << add(5.5, 6, 12);
    return 0;
}

// Function definitions
int add(int a, double b) { 
    return a + (double)b;
}  
double add(double a, int b, int c) {
    return a + (double)b + (double)c;
}  

Output
12
23.5

Functions that Cannot be Overloaded

In C++, we have some specific scenarios in which the functions cannot be overloaded. As function overloading is primarily based on the function name and the types or number of its parameters. There could be cases where the C++ compiler cannot differentiate between functions and thus cannot be overloaded.

  • Member function declarations with different access specifier but same name and same parameter-type-list cannot be overloaded.
  • Functions with parameter declarations that differ only in a pointer * versus an array [].
  • Functions with same parameter but different passing method (i.e. passed by value and passed by reference).
  • Function with same parameter but different volatile type specifier.

Why functions cannot be overloaded based on return type?

C++ doesn't allow function overloading by changing the return type. It is because the return type is not included in the function call, due to which the compiler won't be able to distinguish between them resulting in ambiguity issue. Therefore, we cannot achieve function overloading by changing the return type only.

Example

C++
#include <bits/stdc++.h>
using namespace std;

// Changing return type only
string get() {
    return "This returns string";
}
int get() {
    return 99;
}

int main() {

    // Calling the function even with specifying
    // the type of return value we want
    string s = get()
    cout << s;
    
    return 0;
}


Output

./Solution.cpp: In function 'int get()':
./Solution.cpp:8:9: error: ambiguating new declaration of 'int get()'
int get() {
^
./Solution.cpp:5:8: note: old declaration 'std::__cxx11::string get()'
string get() {
^
./Solution.cpp: In function 'int main()':
./Solution.cpp:17:5: error: expected ',' or ';' before 'cout'
cout << s;

Refer to this article to know more - Function Overloading and Return Type in C++

How Function Overloading Works?

Function overloading in C++ allows multiple functions to have the same name but differ in the number or type of their parameters. The compiler determines the correct overloaded function by evaluating the function signature which consist of the function's name and parameter types and matches it with the passed arguments in the function call.

  • During this process the compiler tries to find an exact match where both the number of arguments and the type of the arguments exactly match the passed parameters.
  • If the compiler doesn't find an exact match, then it tries to find a match by performing type promotion or type conversion of the data type of the passed parameters.
  • If the compiler is unable to find a match for the called function or finds multiple matches for the called function, then it generates an error.

Function Overloading vs Function Overriding

Some of the common difference between Function Overloading and Function Overriding are as follows:

Aspect

Function Overloading

Function Overriding

Definition

Same function name with different parameters.

Same function with same parameters but different function definition.

Purpose

To create multiple functions that perform similar tasks with varying arguments.

To change or extend the behavior of an inherited function.

Function Signature

Function signatures should be different.

Function signature should be similar (name, return type, parameters).

Compile-time or Runtim

Done at compile time

Done at run time

Scope

Overloaded functions are in same scope.

Overridden functions are in different scopes.


Next Article

Similar Reads