Function
Overloading in C++
Learn how C++ lets you define multiple functions with the same name but different parameters: a core technique for writing clean, flexible code.
Quick Refresher: What Is a Function?
A function is a reusable block of code that takes inputs (parameters), performs a task, and optionally returns an output. Every function has a name, a return type, and a parameter list.
The Problem: One Name, One Shape?
Imagine you write an add function for integers. Now you also need to add doubles and add three numbers. Without overloading, you would need unique names for each:
What Is Function Overloading?
Function overloading lets you define multiple functions with the same name in the same scope, as long as their parameter lists differ in at least one of:
The Core Rule
Return Type Alone Is Not Enough
A common misconception: "If I change the return type, that counts as overloading." It does not. The compiler cannot choose between two functions based solely on what they return.
Overloading vs Overriding
| Overloading | Overriding | |
|---|---|---|
| Scope | Same class or scope | Parent and child class |
| Parameters | Must differ | Must be identical |
| Resolved at | Compile time | Run time |
| Polymorphism | Static | Dynamic |
| Keyword needed? | No | virtual / override |
How to Apply Function Overloading
Example 1: Basic Overload
Example 2: Different Number of Parameters
Example 3: Different Order of Types
How the Compiler Decides
Watch Out: Ambiguity from Implicit Conversions
C++ can implicitly promote types (e.g., an \(\texttt{int}\) can be promoted to a \(\texttt{long}\) or a \(\texttt{double}\)). If a call could match multiple overloads via these promotions, and no promotion is considered better than the other, the compiler reports an ambiguous call.
Default Arguments vs Overloading
Sometimes you can achieve similar flexibility with default parameter values instead of separate overloads. But they serve different purposes:
Common Mistakes
Summary: Quick Reference
| Feature | Valid Overload? | Example |
|---|---|---|
| Different param types | ✓ Yes | \(\texttt{f(int)}\) vs \(\texttt{f(double)}\) |
| Different param count | ✓ Yes | \(\texttt{f(int)}\) vs \(\texttt{f(int, int)}\) |
| Different param order | ✓ Yes | \(\texttt{f(int, string)}\) vs \(\texttt{f(string, int)}\) |
| Different return type only | ✕ No | \(\texttt{int f(int)}\) vs \(\texttt{double f(int)}\) |
| Different param names only | ✕ No | \(\texttt{f(int a)}\) vs \(\texttt{f(int b)}\) |
Question 1 of 5
Which of the following is required for two functions to be valid overloads of each other?
Question 2 of 5
What happens if you define \(\texttt{int calc(int x)}\) and \(\texttt{double calc(int x)}\) in the same scope?
Question 3 of 5
Given these overloads:
\(\texttt{void log(string msg)}\)
\(\texttt{void log(string msg, int level)}\)
Which version does \(\texttt{log("Error", 3)}\) call?
Question 4 of 5
Function overloading is an example of which type of polymorphism?
Question 5 of 5
Which pair of function declarations is a valid overload?