Function Overloading in C++
Last Updated :
11 Jan, 2025
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading âFunctionâ name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.
If multiple functions having same name but parameters of the functions should be different is known as Function Overloading.
If we have to perform only one operation and having same name of the functions increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the function such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you to understand the behavior of the function because its name differs.
Function overloading allows you to define multiple functions with the same name but different parameters.
The parameters should follow any one or more than one of the following conditions for Function overloading:
- Parameters should have a different type
add(int a, int b)
add(double a, double b)
Below is the implementation of the above discussion:
C++
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
Outputsum = 12
sum = 11.5
- Parameters should have a different number
add(int a, int b)
add(int a, int b, int c)
Below is the implementation of the above discussion:
C++
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(int a, int b, int c)
{
cout << endl << "sum = " << (a + b + c);
}
// Driver code
int main()
{
add(10, 2);
add(5, 6, 4);
return 0;
}
- Parameters should have a different sequence of parameters.
add(int a, double b)
add(double a, int b)
Below is the implementation of the above discussion:
C++
#include<iostream>
using namespace std;
void add(int a, double b)
{
cout<<"sum = "<<(a+b);
}
void add(double a, int b)
{
cout<<endl<<"sum = "<<(a+b);
}
// Driver code
int main()
{
add(10,2.5);
add(5.5,6);
return 0;
}
Outputsum = 12.5
sum = 11.5
Following is a simple C++ example to demonstrate function overloading.
CPP
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
Output Here is int 10
Here is float 10.1
Here is char* ten
C++
#include<iostream>
using namespace std;
void add(int a, int b)
{
cout<<"sum ="<<(a+b);
}
void add(int a, int b,int c)
{
cout<<endl<<"sum ="<<(a+b+c);
}
main()
{
add(10,2);
add(5,6,4);
return 0;
}
C++
#include<iostream>
using namespace std;
void add(int a, double b)
{
cout<<"sum ="<<(a+b);
}
void add(double a, int b)
{
cout<<endl<<"sum ="<<(a+b);
}
main()
{
add(10,2.5);
add(5.5,6);
return 0;
}
How does Function Overloading work?
- Exact match:- (Function name and Parameter)
- If a not exact match is found:–
->Char, Unsigned char, and short are promoted to an int.
->Float is promoted to double
->C++ tries to find a match through the standard conversion.
Note: In C++, function overloading is primarily based on the function name and the types or number of its parameters. The return type alone does not distinguish overloaded functions. Therefore, changing only the return type of a function without changing its parameters does not create an overload and may lead to a compilation error due to ambiguity.
- Function overloading and return type
- Functions that cannot be overloaded in C++
- Function overloading and const keyword
- Function Overloading vs Function Overriding in C++
Recent articles on function overloading in C++
Learn C++ Online – Master everything from basics to advanced concepts. Apply your knowledge with practical exercises and boost your skills. Take the Three 90 Challenge: finish 90% in 90 days for a 90% refund. Start now and level up your C++ programming!
Similar Reads
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesnât play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); CPP // CP
3 min read
error: call of overloaded âfunction(x)â is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
Function Overloading vs Function Templates in C++
In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, and understand when to use each.Table of ContentFu
4 min read
Function Overloading and Return Type in C++
Function overloading is possible in C++ and Java but only if the functions must differ from each other by the types and the number of arguments in the argument list. However, functions can not be overloaded if they differ only in the return type. Why is Function overloading not possible with different return types and identical parameters? Functi
2 min read
Function overloading and const keyword
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading âFunctionâ name should be the same and the arguments should be different. Function overloading
5 min read
Function Overloading and float in C++
Although polymorphism is a widely useful phenomena in C++ yet it can be quite complicated at times. For instance consider the following code snippet: CPP #include<iostream> using namespace std; void test(float s,float t) { cout << "Function with float called "; } void test(int s, int t) { cout << "Function with int
2 min read
Function Overloading in C++
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading âFunctionâ name should be the same and the arguments should be different. Function overloading
4 min read
How to Use Default Arguments in Function Overloading in C++?
In C++, we can provide the default values for the input arguments into the functions and it is also supported in function overloading. In this article, we will learn how to use default arguments in function overloading in C++. Default Arguments in Function Overloading in C++We can define the default value in function overloading as we do it in the
2 min read
Overloading function templates in C++
Template: A template is a tool that reduces the efforts in writing the same code as templates can be used at those places.A template function can be overloaded either by a non-template function or using an ordinary function template. Function Overloading: In function overloading, the function may have the same definition, but with different argumen
3 min read
Advantages and Disadvantages of Function Overloading in C++
Function overloading is one of the important features of object-oriented programming. It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions. Advantages of function overloading are as follows
3 min read