Skip to content

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.

Beginner 12 min read·Lesson 9 of 40 in C++ Programming

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

C++
double area(double r); // circle
double area(double w, double h); // rectangle
void print(int x);
void print(const std::string& s);
  • Parameter names don't matter for overloading — only types.
  • Default arguments can conflict with overloads; avoid mixing them carelessly.
  • const on a by-value parameter does not create a different overload.

Code examples

Area overloads

C++
#include <iostream>
double area(double r) { return 3.14159 * r * r; }
double area(double w, double h) { return w * h; }
int main() {
std::cout << area(2.0) << ' ' << area(3.0, 4.0) << '\n';
return 0;
}
Output
12.5664 12

Choosing by type

C++
#include <iostream>
#include <string>
void describe(int) { std::cout << "int\n"; }
void describe(double) { std::cout << "double\n"; }
void describe(const std::string&) { std::cout << "string\n"; }
int main() {
describe(5);
describe(5.0);
describe(std::string("5"));
describe('5'); // char promotes to int
return 0;
}
Output
int
double
string
int

More examples

Overloads for a statistics helper

C++
#include <iostream>
#include <vector>
int total(const std::vector<int>& v) {
int s = 0;
for (int x : v) s += x;
return s;
}
double total(const std::vector<double>& v) {
double s = 0;
for (double x : v) s += x;
return s;
}
int total(int a, int b, int c) { return a + b + c; }
int main() {
std::cout << total(std::vector<int>{1, 2, 3}) << '\n';
std::cout << total(std::vector<double>{0.5, 0.25}) << '\n';
std::cout << total(4, 5, 6) << '\n';
return 0;
}
Output
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:

TextOutput
6
0.75
15

Practical applications

  • std::to_string is overloaded for every numeric type.
  • Constructors are overloaded so objects can be built in several ways.
  • Logging libraries overload log for 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

int parse(const std::string& s);
double parse(const std::string& s);

Prefer

int parse_int(const std::string& s);
double parse_double(const std::string& s);

The compiler cannot choose by return type; this is a redefinition error.

Ambiguous calls

Avoid

void f(int);
void f(double);
f(10L); // long: ambiguous

Prefer

f(static_cast<int>(10L));

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

  1. 1
    Overload max_of for two ints, three ints and a vector of ints.
  2. 2
    Write print overloads 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?
Practise this topic