Topic 24 of 91 4. Functions in C++ Interactive Tutorial

Function Overloading in C++: Multiple Signatures for One Function Name

14 mins read Last updated: 26 Sep 2026 21,200 views

Direct Answer & Core Concept

Function overloading in C++ allows multiple functions in the same scope to share the same name with different parameter signatures (different parameter counts, types, or const/reference qualifiers). The compiler resolves overloads at compile-time by matching argument types and uses "name mangling" (encoding parameter types into symbol names in the binary).

Detailed Explanation & Mechanics

1. Rules of Function Overloading:

  • Must differ in number of parameters or parameter types.
  • Overloading based SOLELY on return type is INVALID and causes compilation errors.
  • Name Mangling: The compiler generates distinct assembly symbols like _Z3addii for add(int, int) and _Z3adddd for add(double, double).

```text
OVERLOAD RESOLUTION STEPS:

1. Candidate Functions (same name in scope)

2. Viable Functions (matching parameter count and convertible types)

3. Best Match (exact match > promotion > standard conversion > user-defined conversion)

```

Runnable Code Example: Function Overloading in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>

// Overload 1: Integer addition
int calculateSum(int a, int b) {
    return a + b;
}

// Overload 2: Double addition
double calculateSum(double a, double b) {
    return a + b;
}

// Overload 3: String concatenation
std::string calculateSum(const std::string& a, const std::string& b) {
    return a + " " + b;
}

int main() {
    std::cout << "Int Sum: " << calculateSum(10, 20) << "
";
    std::cout << "Double Sum: " << calculateSum(5.25, 3.75) << "
";
    std::cout << "String Concatenation: " << calculateSum(std::string("Hello"), std::string("C++")) << "
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Can two functions be overloaded if only their return types differ?

A: No. Function call statements (e.g. `calculate();`) do not specify expected return types, making ambiguous calls impossible to resolve.
Q

Q: What is name mangling in C++?

A: Name mangling is the compiler technique of decorating function names with type codes for their arguments so the linker can differentiate overloaded functions with identical source names.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Do not overload functions with ambiguous implicit conversions (e.g. f(int) and f(long) when called with short).
2. Keep the semantic purpose identical across all overloads of a function name.
3. Use templates instead of writing dozens of identical overloads for different types.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.