Module 8: FunctionsBeginner50 min read
Functions, Parameter Passing, Overloading & Inline/Constexpr
Learn function declarations vs definitions, pass-by-value vs reference vs pointer, default arguments, overloading rules, inline, constexpr, and noexcept.
Key Takeaways & Core Concepts
- Pass by Value: Copies argument; safe for small primitives (
int,double). - Pass by `const` Reference (`const T&`): Zero-copy read-only access—default choice for
std::string,std::vector, and classes.
1. Declarations, Definitions & Parameter Passing
- Pass by Value: Copies argument; safe for small primitives (
int,double). - Pass by `const` Reference (`const T&`): Zero-copy read-only access—default choice for
std::string,std::vector, and classes. - Pass by Reference (`T&`) / Pointer (`T
)**: Allows mutating caller state (T*can benullptr`).
1. Const Reference, Overloading & constexpr
1#include <iostream>
2#include <string>
3
4constexpr int cube(int x) noexcept { return x * x * x; }
5void greet(const std::string& name) { std::cout << "Hello, " << name << "\n"; }
6
7int main() {
8 greet("CodeZov");
9 std::cout << "3^3 = " << cube(3) << "\n";
10 return 0;
11}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
11 lines
Note: Avoids string copy via `const std::string&` and computes `cube(3)` at compile time.
2. Function Overloading & Name Mangling
Overloaded functions share a name but differ in parameter count or types. Internally, the C++ compiler performs Name Mangling (encoding parameter types into linker symbol names) so the linker can distinguish area(int) from area(double).
3. Default Arguments, `inline`, `constexpr` & `noexcept`
- Default arguments must appear from right to left in the parameter list.
constexprenables compile-time evaluation;noexceptpromises no exceptions will escape.
Practice & AssessmentC++ Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)