1. Intro
Function overloading allows you to define multiple functions with the same name in the same scope, as long as their parameter lists differ — either in the number of parameters, the types of parameters, or the order of parameter types. The compiler determines which overload to call at compile time by matching the arguments used in each call, a process called overload resolution.
Cricket analogy: Two bowlers can both be called 'Kumar' but the scorer tells them apart by their bowling style — pace versus spin — just as overloaded functions with the same name are told apart by their parameter lists.
2. Syntax
int add(int a, int b);
double add(double a, double b);
int add(int a, int b, int c);
// All three share the name 'add' but differ in parameter list3. Explanation
Overloading is resolved purely by looking at the arguments in a call — their number and types — and matching them to the closest-fitting function signature among the overloads. This happens at compile time (this is a form of static/compile-time polymorphism), unlike virtual functions which resolve at runtime. Crucially, functions cannot be overloaded based on return type alone — two functions with identical parameter lists but different return types are treated as duplicate declarations and cause a compile error, because the compiler has no way to decide which one you meant when the call itself does not use the return value to distinguish them.
Cricket analogy: The third umpire reviews only the delivery in question — its line and height — to decide the call, just as overload resolution looks only at a call's arguments to pick the matching function, never the eventual scoreboard outcome.
Common misconception: int add(int a, int b); and double add(int a, int b); are NOT valid overloads — they differ only in return type with an identical parameter list, so this is a compile-time error ('function already has a body' / redefinition), not valid overloading.
4. Example
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
int main() {
cout << "add(2, 3): " << add(2, 3) << endl;
cout << "add(2.5, 3.5): " << add(2.5, 3.5) << endl;
cout << "add(1, 2, 3): " << add(1, 2, 3) << endl;
return 0;
}5. Output
add(2, 3): 5
add(2.5, 3.5): 6
add(1, 2, 3): 66. Key Takeaways
- Function overloading lets multiple functions share the same name if their parameter lists differ.
- Overload resolution happens at compile time, based on the number and types of the arguments used in the call.
- Functions cannot be overloaded based on return type alone with an identical parameter list.
- Overloading is a form of compile-time (static) polymorphism.
Practice what you learned
1. What must differ between two overloaded functions with the same name?
2. When does C++ resolve which overloaded function to call?
3. Which pair of declarations is a valid overload?
4. Why can't functions be overloaded based on return type alone?
5. What term describes the compiler's process of choosing the correct overloaded function for a call?
Was this page helpful?
You May Also Like
Functions in C++
Learn what a function is in C++, why programs are broken into functions, and how reusability, modularity, and readability improve with them.
Default Arguments in C++
Learn how to give function parameters default fallback values in C++ and the rule that default arguments must be trailing parameters.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics