Basic LevelFUNCTIONS · C++ TrackCompiler: g++ -std=c++20 -O2
C++ Functions & Recursion
Pass-by-reference semantics, call stacks, and recursive algorithms.
Exercises:4 Practice Labs
All C++ ModulesFunctions Coding Labs (4)Click any card to start in laboratory
Lab #01 · Functions
Easy10 mins
06. Functions & Pass-by-Reference
Understand pass-by-value vs pass-by-reference using `&` references.
Objective: Write a function `void swapValues(int& a, int& b)` that swaps two integers in place using references without returning any value.
Interactive SandboxStart Lab
Lab #02 · Functions & Loops
Easy12 mins
Functions: Modular 2D Geometric Shapes Printer
Implement separate C++ functions to print solid squares, hollow rectangles, and right-angled triangles using nested loops.
Objective: Implement modular C++ functions to print geometric star (`*`) patterns:
1. **Square & Rectangle Functions**:
- `void printSquare(int size)`: Prints an `n x n` solid star grid.
- `void printHollowRectangle(int width, int height)`: Prints a hollow rectangle where outer border cells are `*` and inner cells are spaces (` `).
2. **Triangle Functions**:
- `void printRightTriangle(int height)`: Prints a left-aligned right triangle of stars of given height.
- `void printInvertedTriangle(int height)`: Prints an inverted right triangle.
3. **In `main()`**:
- Call `printSquare(4)`.
- Call `printHollowRectangle(6, 4)`.
- Call `printRightTriangle(5)`.
- Call `printInvertedTriangle(4)`.
Interactive SandboxStart Lab
Lab #03 · Functions & Loops
Medium15 mins
Functions: Centered Pyramids, Diamonds & Hourglass Shapes
Write specialized functions to construct centered pyramids, symmetrical diamonds, and hollow hourglass geometric figures.
Objective: Write parameterized C++ functions to construct centered geometric figures:
1. **Pyramid Function**:
- `void printPyramid(int height)`: Prints a centered equilateral triangle where row `i` (from 1 to `height`) has `height - i` leading spaces followed by `2 * i - 1` stars.
2. **Diamond Function**:
- `void printDiamond(int n)`: Prints a symmetric diamond with `n` upper rows (including center) and `n - 1` lower rows.
3. **Hourglass Function**:
- `void printHourglass(int n)`: Prints an hourglass figure composed of an inverted pyramid followed by an upright pyramid.
4. **In `main()`**:
- Call `printPyramid(4)`.
- Call `printDiamond(4)`.
- Call `printHourglass(4)`.
Interactive SandboxStart Lab
Lab #04 · Recursion
Medium12 mins
07. Recursion & Divide-and-Conquer
Compute the nth Fibonacci number recursively.
Objective: Write a recursive function `int fib(int n)` where `fib(0)=0`, `fib(1)=1`, and `fib(n) = fib(n-1) + fib(n-2)`.
Interactive SandboxStart Lab