Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 8

Function Overloading & Default Arguments

**Overloading** allows multiple functions with the same name but different parameter lists. The compiler selects the best match via overload resolution - a complex set of rules involving exact matches, promotions, and conversions. **Default arguments** allow callers to omit trailing parameters, but they must be specified in the declaration (not the definition if they're split).

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Overloading Resolution & Default Arguments

Overloading Resolution & Default Arguments
#include <iostream>
#include <string>
#include <cmath>

// ── Function overloading ──────────────────────────────────────
// Same name, different parameter TYPES or COUNT
int    max_val(int a, int b)       { return a > b ? a : b; }
double max_val(double a, double b) { return a > b ? a : b; }
int    max_val(int a, int b, int c){ return max_val(max_val(a,b), c); }

// ── Ambiguous overloads - compiler error ──────────────────────
// void print(int x)   { }
// void print(long x)  { }
// print(42L);  ← ambiguous: matches both (long→int, long match)

// ── Default arguments - trailing params only ─────────────────
// Declaration sets the default (ONCE, in header)
void log_message(const std::string& msg,
                 const std::string& level = "INFO",
                 bool               timestamp = true);

// Implementation - do NOT repeat defaults
void log_message(const std::string& msg,
                 const std::string& level,
                 bool               timestamp) {
    if (timestamp) std::cout << "[2026-01-01] ";
    std::cout << "[" << level << "] " << msg << "
";
}

// ── Overloading on const ──────────────────────────────────────
struct Buffer {
    char data[64]{};
    char*       get()       { return data; }  // non-const: returns mutable
    const char* get() const { return data; }  // const: returns read-only
};

// ── Overloading vs default args - when to use which ──────────
// Default args: same algorithm, just missing some inputs
// Overloading: different algorithms for different types
void draw(int x, int y, int size = 10, int color = 0xFFFFFF) {}  // default args
// vs
void process(int value)    { /* int-specific */ }
void process(double value) { /* float-specific */ }

int main() {
    std::cout << max_val(3, 7)        << "
";  // 7 (int version)
    std::cout << max_val(3.1, 7.9)    << "
";  // 7.9 (double version)
    std::cout << max_val(1, 5, 3)     << "
";  // 5 (three-arg version)

    log_message("Server started");               // INFO + timestamp
    log_message("Low memory", "WARN");           // WARN + timestamp
    log_message("Raw event", "DEBUG", false);    // DEBUG, no timestamp

    Buffer b;
    b.get()[0] = 'H';  // calls non-const version
    const Buffer cb;
    const char* p = cb.get();  // calls const version
    (void)p;

    return 0;
}

Common Mistakes

  • Putting default arguments in both declaration and definition - `void f(int x = 5)` in header AND `void f(int x = 5) { }` in .cpp causes a compile error 'default argument given for parameter'. Defaults go only in the declaration.
  • Overloading on return type alone - `int f()` and `double f()` is NOT valid overloading - the return type is not part of the overload signature. Only parameter types and count distinguish overloads.
  • Overload resolution with implicit conversions causes surprises - `void f(int)` and `void f(long)` - calling `f(42UL)` (unsigned long) creates ambiguity. Be explicit: `f(static_cast<long>(42UL))`.

Tip

Tip

Practice Function Overloading Default Arguments in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Compile-Time Polymorphism via Parameter Signatures and Default Arguments Rules

Practice Task

Note

Practice Task - (1) Write a working example of Function Overloading Default Arguments from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Function Overloading Default Arguments is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Key Takeaways

  • **Overloading** allows multiple functions with the same name but different parameter lists.
  • Putting default arguments in both declaration and definition - `void f(int x = 5)` in header AND `void f(int x = 5) { }` in .cpp causes a compile error 'default argument given for parameter'. Defaults go only in the declaration.
  • Overloading on return type alone - `int f()` and `double f()` is NOT valid overloading - the return type is not part of the overload signature. Only parameter types and count distinguish overloads.
  • Overload resolution with implicit conversions causes surprises - `void f(int)` and `void f(long)` - calling `f(42UL)` (unsigned long) creates ambiguity. Be explicit: `f(static_cast<long>(42UL))`.

Topics in This Module

0/8 done