C++ Tutorial: Functions
Declare and call functions, pass by value vs reference, default arguments, overloading, inline, and recursion.
Don't repeat yourself
Functions are named, reusable blocks of logic. In C++, function parameters are **copied by default** — unlike Python and Java where objects are references. Understanding pass-by-value vs pass-by-reference is one of the most important C++ concepts.
What you'll learn in this C++ functions tutorial
This interactive C++ tutorial has 7 hands-on exercises. Estimated time: 20 minutes.
- Your first function — Declare functions BEFORE `main()` or provide a **forward declaration**:
- Pass by value — copies are made — In C++, function parameters are **copied** by default. Changing them inside the function doesn't affect the caller:
- Pass by reference — modify the original — Add `&` to make a parameter a **reference** — the function operates on the original variable:
- Default arguments — Parameters can have default values — used when the caller doesn't provide them:
- Function overloading — Multiple functions with the same name but different parameter types or counts.
- Recursion — A function that calls itself. Every recursive function needs:
- Build string utilities — No starter code. Write three functions:
C++ Functions concepts covered
- Don't repeat yourself