Functions & Overloading
c1-5's functions had one name each, always. This chapter introduces the first genuinely new C++ capability that has no C equivalent at all — not a missing feature, a structural impossibility in C's own linking model.
Function Overloading
Multiple functions can share the same name, distinguished by their parameter types or count — the compiler picks the right one based on the actual arguments at each call site.
C cannot do this at all. C compiles every function name directly to one plain, unmangled linker symbol — two functions named print in the same C program is simply a redefinition error, per c2-5's own linking material.
Name Mangling, Briefly
C++ solves this by encoding parameter types into the actual linker symbol name. print(int) and print(double) become genuinely different symbols under the hood — something like _Z5printi and _Z5printd.
This directly extends c2-5's own linker material: overloading isn't possible because the linker got smarter — it's possible because C++ generates different names for what looks, in source, like the same function name.
extern "C"
A real, practical need: calling a C library from C++, or being called from C code, requires plain, unmangled names. extern "C" tells the compiler to use C's own naming rules for specific declarations — this is exactly why standard C headers are internally wrapped in extern "C" when included from C++.
Default Arguments
A genuinely new feature: parameters can have default values, letting a function be called with fewer arguments than it declares.
C has no equivalent at all — the closest workaround is a variadic function, or several separately-named functions.
Overload Resolution Ambiguity
If a call could plausibly match more than one overload after implicit conversions, the compiler reports an ambiguous call error rather than guessing.
| Concept | C | C++ |
|---|---|---|
| Two functions, same name | redefinition error | valid — overloading, resolved by parameter types |
| Linker symbol names | plain function name | mangled — encodes parameter types |
| Fewer arguments than declared | not possible — variadic functions or multiple names only | default arguments |
extern "C" is needed specifically because name mangling would otherwise make a C++-compiled function unreachable from C's own, unmangled linker expectations.
Coding Challenges
Write two overloaded describe functions, one taking an int and one taking a std::string, each printing a different message. Call both from main and confirm the correct overload is selected each time.
📄 View solutionWrite a function power(int base, int exponent = 2) with a default argument, and call it once with both arguments and once with only the base, printing both results.
📄 View solutionExplain precisely why C cannot support function overloading at all — tying your answer to how C's linker resolves symbol names — and why extern "C" is necessary when C++ code needs to interoperate with C.
📄 View solutionChapter 3 Quick Reference
- Overloading — same function name, different parameter types/count, resolved at the call site
- Name mangling — the compiler encodes parameter types into the real linker symbol, making overloading possible
extern "C"— forces plain, unmangled C-style linkage for interop with C libraries/callers- Default arguments — trailing parameters only; can't skip a middle one and provide a later one
- An ambiguous overload call is a compile error, never a silent guess
- Next chapter: classes and objects — the leap beyond plain structs