Ch 8 · Functions · C++
Topic 8 of 13 in C++ — Complete Tutorial (Beginner) — 2 lessons.
Functions, Defaults & Overloading
A function is a named, reusable block of code. In C++ you declare its return type, name, and parameters. C++ adds two handy features: default parameters (a value used when the caller omits an argument) and overloading (several functions sharing a name but differing in parameters, with the compiler picking the right one). These make functions flexible and expressive.
Pass by Value vs Reference
By default, C++ passes a copy of an argument, so changes inside the function do not affect the original. Adding & to a parameter makes it a reference — an alias for the caller's variable — so the function can modify the original directly and avoids copying large data. Use a const reference (const T&) to pass big values efficiently without allowing changes.