100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Function Overloading in C++

Learn how C++ resolves multiple same-named functions at compile time based on parameter lists, and why return type alone cannot distinguish overloads.

FunctionsIntermediate7 min readJul 7, 2026
Analogies

1. Intro

Function overloading allows you to define multiple functions with the same name in the same scope, as long as their parameter lists differ — either in the number of parameters, the types of parameters, or the order of parameter types. The compiler determines which overload to call at compile time by matching the arguments used in each call, a process called overload resolution.

🏏

Cricket analogy: Two bowlers can both be called 'Kumar' but the scorer tells them apart by their bowling style — pace versus spin — just as overloaded functions with the same name are told apart by their parameter lists.

2. Syntax

cpp
int add(int a, int b);
double add(double a, double b);
int add(int a, int b, int c);
// All three share the name 'add' but differ in parameter list

3. Explanation

Overloading is resolved purely by looking at the arguments in a call — their number and types — and matching them to the closest-fitting function signature among the overloads. This happens at compile time (this is a form of static/compile-time polymorphism), unlike virtual functions which resolve at runtime. Crucially, functions cannot be overloaded based on return type alone — two functions with identical parameter lists but different return types are treated as duplicate declarations and cause a compile error, because the compiler has no way to decide which one you meant when the call itself does not use the return value to distinguish them.

🏏

Cricket analogy: The third umpire reviews only the delivery in question — its line and height — to decide the call, just as overload resolution looks only at a call's arguments to pick the matching function, never the eventual scoreboard outcome.

Common misconception: int add(int a, int b); and double add(int a, int b); are NOT valid overloads — they differ only in return type with an identical parameter list, so this is a compile-time error ('function already has a body' / redefinition), not valid overloading.

4. Example

cpp
#include <iostream>
using namespace std;

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

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

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

int main() {
    cout << "add(2, 3): " << add(2, 3) << endl;
    cout << "add(2.5, 3.5): " << add(2.5, 3.5) << endl;
    cout << "add(1, 2, 3): " << add(1, 2, 3) << endl;
    return 0;
}

5. Output

text
add(2, 3): 5
add(2.5, 3.5): 6
add(1, 2, 3): 6

6. Key Takeaways

  • Function overloading lets multiple functions share the same name if their parameter lists differ.
  • Overload resolution happens at compile time, based on the number and types of the arguments used in the call.
  • Functions cannot be overloaded based on return type alone with an identical parameter list.
  • Overloading is a form of compile-time (static) polymorphism.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#FunctionOverloadingInC#Function#Overloading#Syntax#Explanation#Functions#StudyNotes#SkillVeris