Functions
More Than Just "Call and Return"
In many languages, a function name is unique β one name, one behavior. C++ breaks that rule in a powerful way. You can have multiple functions with the same name that do different things based on what you pass them. This is called function overloading, and it's one of the features that makes C++ feel expressive.
But that's just the start. C++ also lets you set default argument values, choose whether to copy or reference data, and even create tiny anonymous functions on the fly. Let's explore each of these tools.
Function Overloading β Same Name, Different Blades
Imagine a print() function. Sometimes you want to print an integer. Sometimes a string. Sometimes a vector. In C, you'd need printInt(), printStr(), printVec(). In C++, they can all be called print() β the compiler figures out which one to call based on the number and types of arguments.
Function Overloading in Action
Default Arguments β Pre-Set the Knobs
Sometimes a function has parameters that usually take the same value. Instead of making callers type it every time, you can give parameters default values. Defaults must start from the rightmost parameter and go left β you can't skip parameters in the middle.
Default Arguments
Pass by Value vs. Pass by Reference
This is where C++ gives you control that most languages hide. When you pass an argument to a function, you choose:
- By value β the function gets a copy. Changes inside don't affect the original.
- By reference (&) β the function gets the original. Changes inside modify the caller's data.
- By const reference (const &) β the function gets the original but promises not to modify it. Best of both worlds for read-only access.
Think of it like lending a book. By value = you photocopy the book and give the copy. By reference = you hand over the actual book. By const reference = you hand over the book but say "just read it, don't write in it."
Pass by Reference β The Classic Swap
const Reference β Fast & Safe for Large Objects
const reference (const std::string&, const std::vector& ) β passing by value copies the entire object! Only pass by value for small, cheap-to-copy types like int, char, bool, and double.Inline Functions & auto Return Type
inline is a hint to the compiler: "This function is tiny β please paste its body directly at the call site instead of making a real function call." Modern compilers usually decide this on their own, so inline is mostly used for header-file functions to avoid linker errors.
C++14 introduced auto return types β the compiler deduces the return type from the return statement. Handy for templates and simple functions.
Lambdas β Functions on the Fly
Lambda Syntax Cheat Sheet
The general form is: [capture](parameters) -> return_type { body }
[]β capture nothing from the outside scope[x]β capturexby value (copy)[&x]β capturexby reference[=]β capture everything by value[&]β capture everything by reference
Lambdas are especially useful with STL algorithms like sort, for_each, find_if, and count_if.