Skip to content
Theory · Functions · lesson 7 of 13

Function overloading

The word "open" means different things for a door, a book and a jar: what to do is clear from the object in your hands. C++ works the same way: several functions may share one name, and the compiler picks the right one by the call's arguments.

The function int twice(int x) doubles integers, while 1.25 needs the same function for double. It does not need a different name: in C++ both functions can be called twice:

C++
#include <iostream>

int twice(int x) {
    return x * 2;
}

double twice(double x) {
    return x * 2;
}

int main() {
    std::cout << twice(21) << ' ' << twice(1.25) << '\n';  // 42 2.5
}

For 21 the compiler calls the int version, and for 1.25 the double version. The "open" analogy limps in one place: a person guesses from the meaning, while the compiler looks only at the argument types.

Different parameter lists

Overloading means several functions with one name and different parameter lists. The lists must differ in at least one of two ways:

  • in parameter types, like twice(int) and twice(double) above;
  • in the number of parameters, like max(int a, int b) and max(int a, int b, int c).

The return type plays no part. The compiler picks a function by the call's arguments alone, and the result does not exist yet when the choice is made.

Choosing an overload

When several overloads fit, the compiler ranks how the argument converts to the parameter of each one. The ways go from best to worst:

  1. Exact match: the argument's type equals the parameter's type.
  2. Promotion: widening float to double, or a small integer type such as short to int.
  3. Conversion: any other change of type, such as float → int or int → double.

An argument of type short goes to twice(int): it is promoted to int but converted to double.

The literal 2.5f has type float, while 2.5 is a double. So twice(2.5f) calls twice(double): a float is promoted to double but converted to int.

If the two best overloads are equally good, the call is ambiguous, and the program does not compile. That happens with an argument of type long: long → int and long → double are both conversions.

How the compiler picks an overload is a question interviewers like to ask. Answer with the ranking: exact match, then promotion, then conversion.

Common mistake

Two functions differ only in their return type:

C++
int parse(int x);
double parse(int x);  // ✗
TEXT
error: functions that differ only in their return type cannot be overloaded

This is a compiler error: the call parse(5) does not show which of the two results is wanted. Give the functions different names or different parameter lists.

Key points

  • Overloading means functions with one name and different parameter lists: by types or by number.
  • The return type does not tell overloads apart: such functions do not compile.
  • The compiler prefers an exact match to a promotion, and a promotion to a conversion.
  • Two equally good overloads make a call ambiguous, and the program does not compile.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way