C++ · Module 5 · Lesson 31 min read

Default Arguments & Overloading

Default arguments

scss
double add_tax(double amount, double rate = 0.13);
add_tax(1000);        // rate = 0.13
add_tax(1000, 0.05);  // rate = 0.05

Rules

  • Defaults must be the rightmost parameters: void f(int a, int b = 2, int c = 3); ✅, void f(int a = 1, int b); ❌
  • Put the defaults in the declaration (header), not repeated in the definition
  • You can't skip a middle argument: f(1, , 3) ❌

Function overloading

Several functions with the same name but different parameter lists:

arduino
int area(int side);                      // square
int area(int width, int height);         // rectangle
double area(double radius);              // circle

The compiler picks the best match from the argument types and count (overload resolution).

What counts as "different"

  • ✅ Number of parameters
  • ✅ Types of parameters
  • ✅ const& vs & reference parameters
  • ❌ Return type alone does NOT make an overload

Ambiguity ⚠️

php
void print(int);
void print(double);
print(5L);   // long → int or double? ambiguous → error

Fix with an explicit cast or another overload.

Overloading vs default arguments

  • Same logic with optional extras → default arguments
  • Different logic for different types → overloading
  • Many types, same logic → templates (Unit 12)

Name mangling

The compiler gives each overload a unique internal name (e.g. _Z4areai, _Z4areaii). That's why C++ functions called from C need extern "C".

Example

C++
#include <iostream>
#include <string>

constexpr double PI = 3.14159265358979;

double add_tax(double amount, double rate = 0.13) { return amount * (1 + rate); }

void log(const std::string& msg, const std::string& level = "INFO", int indent = 0) {
    std::cout << std::string(indent, ' ') << "[" << level << "] " << msg << '\n';
}

// Overloads: same name, different parameters
int area(int side) { return side * side; }
int area(int width, int height) { return width * height; }
double area(double radius) { return PI * radius * radius; }

void describe(int x) { std::cout << "int: " << x << '\n'; }
void describe(double x) { std::cout << "double: " << x << '\n'; }
void describe(const std::string& x) { std::cout << "string: " << x << '\n'; }
void describe(char x) { std::cout << "char: " << x << '\n'; }

int main() {
    std::cout << "add_tax(1000)       = " << add_tax(1000) << '\n';
    std::cout << "add_tax(1000, 0.05) = " << add_tax(1000, 0.05) << '\n';

    log("App started");
    log("Low balance", "WARN");
    log("Retrying sync", "DEBUG", 4);

    std::cout << "square 5: " << area(5) << ", rectangle 4x6: " << area(4, 6)
              << ", circle r=2: " << area(2.0) << '\n';

    describe(42);
    describe(4.2);
    describe(std::string("forty-two"));
    describe('Z');
    describe(static_cast<int>(5L));      // cast avoids an ambiguous call
    return 0;
}

/* Output:
add_tax(1000)       = 1130
add_tax(1000, 0.05) = 1050
[INFO] App started
[WARN] Low balance
    [DEBUG] Retrying sync
square 5: 25, rectangle 4x6: 24, circle r=2: 12.5664
int: 42
double: 4.2
string: forty-two
char: Z
int: 5
*/