devnotes.

Functions and Function Overloading

C++ functions look identical to C's, but C++ adds function overloading — something C does not allow.

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

double add(double a, double b) {    // same name, different parameter types
    return a + b;
}

int add(int a, int b, int c) {      // same name, different parameter count
    return a + b + c;
}

add(2, 3);        // calls the (int, int) version -> 5
add(2.5, 3.5);     // calls the (double, double) version -> 6.0
add(1, 2, 3);      // calls the (int, int, int) version -> 6

The compiler chooses which overload to call based on the number and types of the arguments at the call site — this is resolved entirely at compile time (called "static" or "compile-time" polymorphism), unlike the virtual-function-based polymorphism covered later, which is resolved at runtime.

Overload resolution can fail with an ambiguous call error if the compiler can't determine a single best match — for instance, calling add(2, 3.0) when only add(int, int) and add(double, double) exist requires converting one argument, and if multiple equally-good conversions exist, the compiler refuses to guess and reports an error rather than picking arbitrarily. Function overloading is how many standard library functions (and operators, covered later) present a single, natural-feeling name for logically-the-same operation across different types.

Function Overloading in C++ | DevNotes