Table of contents
1.
Introduction 
2.
What is function overloading?
3.
Function overloading in C++
3.1.
Based on different number of parameters
3.1.1.
Code
3.2.
Based on different types of parameters
3.2.1.
Code
3.3.
Based on different sequences of parameters
3.3.1.
Code
4.
Causes of function overloading in c++
4.1.
Type conversion
4.1.1.
Code
4.2.
Function with default arguments
4.2.1.
Code
4.3.
Function with a pass-by-reference
4.3.1.
Code
5.
Frequently asked questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Function overloading

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Function overloading is a feature of object-oriented programming, where two or more functions can have the same name but different parameters. We also call function overloading compile-time polymorphism because function overloading resolves at compile time.

The main advantage of Function overloading is to increase the program's readability so that you don't need to use other names for doing the same task.

Also, see Literals in C. Fibonacci Series in C++

What is function overloading?

In function overloading, different parameters mean different types of parameters, different numbers of parameters, or a different sequence of parameters. For example, we have two functions fun(int x, int y) and fun(int x, int y, int z), which have the same name but a different number of parameters. Function overloading can also have the same function name with different types of parameters.

For example:

int sum(int a,int b)
{
     return a+b;
}
float sum(float a,float b)
{
      return a+b;
}

In the above code, the two functions have the same name, but they use different parameters.

Click on the following link to read further: Features of C Language

Function overloading in C++

Function overloading based on different parameters:

Based on different number of parameters

In this method, we define two or more functions with the same name and have a different number of parameters.

Code

#include <iostream>
using namespace std;
class multiplication
 {
    public:
    int mul(int x,int y) //1
    {
        cout<<" function 1 is called ";
        return x*y;
    }
    int mul(int x,int y, int z) //2
    {
        cout<<" function 2 is called ";
       return x*y*z;
    }
 };
int main()
{
    multiplication M;     //class object decleration
    cout<<M.mul(2, 14, 6)<<endl;
    cout<<M.mul(10, 12)<<endl;
    cout<<M.mul(5, 10, 12);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output: 

function 2 is called 168
 function 1 is called 120
 function 2 is called 600

In the above code, we define a mul() function two times. First, mul() have two parameters, second mul() function has three parameters. The mul() function is called based on the number of parameters.

You can try and compile with the help of online c++ compiler.

Based on different types of parameters

In this method, we define two or more functions with the same name, and they have different return types or different types of parameters.

Code

#include <iostream>
using namespace std;
class multiplication
 {
    public:
    int mul(int x, int y) //1
    {
        cout<<" function 1 is called ";
        return x*y;
    }
    double mul(double x,double y) //2 
    {
        cout<<" function 2 is called ";
       return x*y;
    }
 };
int main()
{
    multiplication M;     //class object decleration
    cout<<M.mul(8.2, 9.5)<<endl;
    cout<<M.mul(10, 12)<<endl;
    cout<<M.mul(5.6, 10.5);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output: 

function 2 is called 77.9
function 1 is called 120
function 2 is called 58.8

In the above code, we define a mul() function two times. First, mul() first uses integers as parameters, while the second mul() function uses double as parameters. The mul() function is called based on the types of parameters.

Based on different sequences of parameters

In this method, we define two or more functions with the same name and have different sequences of parameters.

Code

#include <iostream>
using namespace std;
class multiplication
 {
    public:
    double mul(int x, double y) //1
    {
        cout<<" function 1 is called ";
        return x*y;
    }
    double mul(double x, int y) //2
    {
        cout<<" function 2 is called ";
        return x*y;
    }
    double mul(double x, double y) //3
    {
        cout<<" function 3 is called ";
        return x*y;
    }
 };
int main()
{
    multiplication M;    //class object decleration
    cout<<M.mul(13, 12.3)<<endl;
    cout<<M.mul(9.6, 4.3)<<endl;
    cout<<M.mul(5.6, 8);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output: 

function 1 is called 159.9
function 3 is called 41.28
function 2 is called 44.8

In the above code, we define a mul() function three times, but each mul() functions have a different sequence of parameters. The mul() function is called based on the sequence of parameters.

Causes of function overloading in c++

  1. Type Conversion.
  2.  Function with default arguments
  3.  Function with a pass-by-reference.

Type conversion

Code

#include<iostream>  
using namespace std;  
void print(int);  
void print(float);  
void print(int x)  
{  
    cout<<"x="<<x<<endl;  
}  
void print(float y)  
{  
    cout<<"y="<<y<<endl;  
}  
int main()  
{  
    print(92);  
    print(10.2);  
    return 0;  
}
You can also try this code with Online C++ Compiler
Run Code

In the above code, we get an error “call of overloaded ‘print(double)’ is ambiguous.” According to the above, learning print(10.2) will call the second function for its execution, but that is not going to happen. This is because the compile will take 10.2 as a double data type, and we didn’t write any function that overloads the double data type. 

Function with default arguments

Code

#include<iostream>  
using namespace std;  
void print(int);  
void print(int,int);  
void print(int x)  
{  
    cout<<"x="<<x<<endl;  
}  
void print(int x, int y=15)  
{  
    cout<<"x="<<x<<endl; 
    cout<<"y="<<y<<endl;  
}  
int main()  
{  
    print(12);  
    print(14);  
    return 0;  
}
You can also try this code with Online C++ Compiler
Run Code

In the above code, we get an error “call of overloaded ‘print(int)’ is ambiguous.” This is because the compiler cannot distinguish between the first and second functions. After all, both the function is written in a way that they are going to accept one integer.

Function with a pass-by-reference

Code

#include<iostream>  
using namespace std;  
void print(int);  
void print(int&);  
void print(int x)  
{  
    cout<<"x="<<x<<endl;  
}  
void print(int &y)  
{  
    cout<<"y="<<y<<endl; 
}  
int main()  
{  
    int x=42;
    print(12);  
    print(x);  
    return 0;  
}
You can also try this code with Online C++ Compiler
Run Code

In the above code, we get an error “call of overloaded ‘print(int&)’ is ambiguous.” As clear from the function code, one function will take an integer as input while the other takes a reference as a parameter. The compiler gets confused and unable to call any one of them for you instead of no error in the program.

 

Check out this article - Compile Time Polymorphism

Frequently asked questions

  1. What is function overloading?
    Ans: Function overloading is a feature of C++, where two or more functions can have the same name but different parameters.
     
  2. Does C support function overloading like C++?
    Ans: C does not support function overloading like C++.  
     
  3. What is the use of function overloading?
    Ans: The function overloading is used to improve the readability of the code.

Key Takeaways

That's all about function overloading. This blog is over, but the thirst for learning is not over yet. 

Check out more related articles: 

Continue your journey of acquiring knowledge with our practice platform Coding Ninjas Studio to learn, attempt, read interview experiences, and much more. Till then, have a nice day and Happy Coding! 

Live masterclass