Module 3 · Functions and References — topic 2 of 5
Function Overloading
Several functions may share a name if their parameter lists differ. Learn how the compiler picks the best match and how ambiguity arises.
Learning objectives
- Write overloaded functions
- Explain how overload resolution chooses a function
- Recognise ambiguous calls
Definition
Function overloading is the declaration of multiple functions with the same name in the same scope but different parameter lists; overload resolution selects the best match based on argument types at compile time.
Introduction
In C you need abs, labs, fabs for different types. In C++ one name can serve all of them: you write several functions called area or print that take different parameters, and the compiler calls the right one.
Why it matters
Overloading makes APIs natural: print(42), print(3.14), print("hi").
It is the foundation of operator overloading and constructors with different parameter lists.
Detailed explanation
Overloads must differ in the number or types of parameters. A different return type alone is not enough.
The compiler ranks candidates: exact match, then promotion (e.g. char→int, float→double), then standard conversion (e.g. int→double), then user-defined conversion. The best unique match wins.
If two candidates are equally good the call is ambiguous and does not compile — for example f(int) and f(double) called with a long.
Overloading happens at compile time; it is a kind of static polymorphism.
Syntax
- Parameter names don't matter for overloading — only types.
- Default arguments can conflict with overloads; avoid mixing them carelessly.
conston a by-value parameter does not create a different overload.
Code examples
Area overloads
12.5664 12
Choosing by type
int double string int
More examples
Overloads for a statistics helper
6 0.75 15
Step-by-step explanation
- Three functions share the name
total. - The argument
std::vector<int>matches the first exactly. - A
std::vector<double>matches the second. - Three ints match the third by count.
Expected output
Running “Overloads for a statistics helper” prints exactly:
Practical applications
std::to_stringis overloaded for every numeric type.- Constructors are overloaded so objects can be built in several ways.
- Logging libraries overload
logfor different value types.
Best practices
- Overload only when the functions do the same conceptual job.
- Keep overload sets small and obvious.
- Prefer templates when the code is identical for every type.
Common mistakes
Overloading only on return type
Avoid
Prefer
The compiler cannot choose by return type; this is a redefinition error.
Ambiguous calls
Avoid
Prefer
long converts equally well to int and double, so neither is better.
Important notes
- Overloading interacts with templates: a non-template exact match is preferred over a template.
- Name lookup happens before overload resolution; a name in an inner scope hides outer overloads.
Key takeaways
- Overloads differ by parameter types or count, not return type.
- The compiler picks the best match: exact > promotion > conversion.
- Equally good matches are ambiguous errors.
Try it yourself
- 1Overload
max_offor two ints, three ints and a vector of ints. - 2Write
printoverloads for int, double, bool and std::string.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhy do these two declarations conflict? `int parse(std::string s); double parse(std::string s);`
- BeginnerWhen is function overloading resolved?