C++ Function Overloading
In this page:
return_type function_name(type1 param);
return_type function_name(type2 param);
return_type function_name(type1 param1, type2 param2);
Overloading by Parameter Type
C++ allows several functions to share the exact same name as long as their parameter types differ, so void print(int x) and void print(double x) can coexist — the compiler picks the right one automatically based on the argument type you pass at the call site.
This is called function overloading.
Example: Overloading by Parameter Type
// Include std::cout and std::cin
#include <iostream>
void print(int x) { std::cout << "int: " << x << std::endl; }
void print(double x) { std::cout << "double: " << x << std::endl; }
// Program execution starts in main()
int main() {
print(5);
print(5.5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading by Number of Parameters
Functions can also be overloaded by how many parameters they take, so void log(string msg) and void log(string msg, string level) can both exist under the same name — the compiler distinguishes them purely by counting the arguments supplied at each call.
Example: Overloading by Number of Parameters
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void log(std::string msg) { std::cout << msg << std::endl; }
void log(std::string msg, std::string level) { std::cout << "[" << level << "] " << msg << std::endl; }
// Program execution starts in main()
int main() {
log("Started");
log("Started", "INFO");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading by Order of Parameters
Even when two functions share the same set of parameter types, reordering those types — like void combine(int a, double b) versus void combine(double a, int b) — creates two genuinely distinct, valid overloads, since the compiler still sees them as different parameter signatures.
Example: Overloading by Order of Parameters
// Include std::cout and std::cin
#include <iostream>
void combine(int a, double b) { std::cout << "int,double: " << a << "," << b << std::endl; }
void combine(double a, int b) { std::cout << "double,int: " << a << "," << b << std::endl; }
// Program execution starts in main()
int main() {
combine(1, 2.5);
combine(2.5, 1);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Best Practices in Overloading
Overloaded functions with the same name should perform genuinely related, conceptually similar tasks — for example, several area() overloads for different shapes — rather than being unrelated operations that just happen to share a convenient name.
Reusing a name for unrelated behavior makes code harder to understand at a glance.
Example: Best Practices in Overloading
// Include std::cout and std::cin
#include <iostream>
double area(double side) { return side * side; }
double area(double length, double width) { return length * width; }
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << area(4.0) << std::endl;
// Print to the console with cout
std::cout << area(3.0, 5.0) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Resolving Overload Ambiguity
The compiler occasionally can't determine which overload you meant, usually when an argument's type is ambiguous between two candidates — passing a literal 0 where both an int and a pointer overload exist is a classic example.
When this happens, you'll get a compile-time 'ambiguous call' error rather than a silent wrong choice, which at least surfaces the problem immediately.
Example: Resolving Overload Ambiguity
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
int main() {
show(5); // clearly int
show(5.0); // clearly double
// show('a'); // ambiguous-prone: char can convert to either
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Overloading only by return type, which is not allowed.
- Calling an overloaded function with an argument that matches two versions equally, causing an ambiguity error.
- Expecting
intandconst intparameters to be different overloads, when top-level const is ignored.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions