Recursion
Recursion is a function calling itself with a smaller or simpler argument until a base case returns without another call. Each call pushes a stack frame. C++ does not guarantee that a tail call will reuse the frame.
Detailed Explanation
A tree's height is 1 plus the taller child's height. A directory's size is the sum of its children's sizes. Those definitions are recursive, and the code can match them. You write the base case (empty tree, empty directory) and the reduction (look at the children). The call stack remembers the pending additions.
Every recursive function needs a base case that does not recurse, and an argument that moves toward that case. Forget either and you overflow the stack or loop until you do. if (n == 0) return 1; then return n * fact(n - 1); is the textbook factorial. n < 0 should be an error, not another call.
Divide-and-conquer (std::sort's quicksort phase, binary search) splits into independent subproblems. Tree and graph DFS follow edges. Backtracking tries a choice, recurses, then undoes the choice. Dynamic programming is recursion plus a table so each subproblem runs once.
The stack frame holds locals, arguments, and a return address. A 1 MB default stack (some Windows configurations) holds a few thousand frames if each frame is large, or tens of thousands if it is tiny. A linked list of a million nodes walked recursively will crash. The iterative rewrite uses std::stack or a loop and puts the data on the heap.
Tail recursion is a call that is the last thing the function does. Some languages reuse the frame. C++ compilers sometimes do, under optimisation, and sometimes do not. Do not write a "O(1) space" recursive walk and rely on tail-call elimination. Write a loop if space matters.
Mutual recursion (isEven calls isOdd calls isEven) is legal and is harder to reason about. Indirect recursion through a virtual call is legal and will not be tail-call optimised.
Recursion is the right default for trees, for parsers, and for algorithms whose natural definition is recursive. It is the wrong default for a linear scan. Interviews that say "write it recursively" still expect you to know the stack cost.
Code Examples
#include <cstddef>
#include <iostream>
#include <vector>
int sum(const std::vector<int>& v, std::size_t i) {
if (i == v.size()) return 0; // base case
return v[i] + sum(v, i + 1); // smaller index range
}
int main() {
std::vector<int> v{1, 2, 3, 4};
std::cout << sum(v, 0) << '\n'; // prints: 10
}#include <iostream>
int binarySearch(const int* a, int lo, int hi, int target) {
if (lo > hi) return -1;
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) return mid;
if (a[mid] < target) return binarySearch(a, mid + 1, hi, target);
return binarySearch(a, lo, mid - 1, target);
}
int main() {
int a[] = {1, 3, 5, 7, 9};
std::cout << binarySearch(a, 0, 4, 7) << '\n'; // prints: 3
}Common Gotchas
Missing or unreachable base case
`fact(-1)` if you only check `n == 0` recurses until the stack dies. Guard the domain.
C++ tail calls are not a language guarantee
a recursive DFS on a degenerate tree is a production crash. Iterate.
Each frame copies arguments passed by value
`void f(std::vector<int> v, int i)` on every call is O(n²) time and space. Pass `const&`.
Shared mutable state needs undo
backtracking must restore the array after the recursive call, or the next branch sees leftover writes.