01 / 21
\(\texttt{f(x)}\)
\(\texttt{f(x,y)}\)
\(\texttt{int}\)
\(\texttt{double}\)

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.

C++

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.

int add(int a, int b) { return a + b; }

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:

int addInt(int a, int b); double addDouble(double a, double b); int addThree(int a, int b, int c);

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:

Number
Different count of parameters
Type
Different data types of parameters
Order
Different sequence of types

The Core Rule

Overload Resolution: Compiler Decision Flow
Function Call: \(\texttt{add(...)}\)
Match param types & count?
\(\texttt{add(int, int)}\)
Version 1
int, int
\(\texttt{add(double, double)}\)
Version 2
double, double
\(\texttt{add(int, int, int)}\)
Version 3
int, int, int

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.

// ERROR: ambiguous overload int compute(int x); double compute(int x); // same params!

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

1
Identify the shared action. Find a task that logically has the same name but handles different inputs: e.g., "print", "add", "display".
2
Vary the parameter list. Each version must differ in the number, type, or order of parameters.
3
Write each version. Define each function with the same name, returning an appropriate type.
4
Call with the right arguments. The compiler matches the call to the correct version automatically.
5
Avoid ambiguity. Watch out for implicit conversions that might match more than one overload.

Example 1: Basic Overload

int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int main() { cout << add(3, 4); // calls int version -> 7 cout << add(2.5, 3.1); // calls double version -> 5.6 }

Example 2: Different Number of Parameters

void greet() { cout << "Hello, World!"; } void greet(string name) { cout << "Hello, " << name << "!"; } void greet(string name, int times) { for(int i = 0; i < times; i++) cout << "Hello, " << name << "!\n"; }

Example 3: Different Order of Types

void display(int id, string label) { cout << "#" << id << ": " << label; } void display(string label, int id) { cout << label << " (#" << id << ")"; } int main() { display(42, "Widget"); // version 1 display("Widget", 42); // version 2 }

How the Compiler Decides

You write: \(\texttt{add(3, 4)}\)
Step 1: Find all functions named \(\texttt{"add"}\)
Step 2: Match argument types \(\texttt{(int, int)}\)
Step 3: Pick the best match (exact or convert)
Calls: \(\texttt{add(int, int)}\) ✓

Watch Out: Ambiguity from Implicit Conversions

void func(long x); void func(double x); func(10); // int: can be promoted to long OR double // -> AMBIGUOUS CALL!

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:

Overloading
Different logic per version. Different types handled. Full control over each implementation.
Default Args
Same logic, just fewer arguments. One function handles all cases via defaults.

Common Mistakes

✕ Return Type Only
Changing only the return type without changing parameters. This does not create a valid overload.
✕ Ambiguous Conversions
Having overloads like \(\texttt{f(long)}\) and \(\texttt{f(double)}\) then calling with an int, which can be promoted to either type.
✕ Default Arg Clash
An overload with fewer params conflicts with another that has default values, making the call ambiguous.

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?