C++ Functions Quiz: Parameters, Recursion and Lambdas
Test your C++ functions knowledge with 14 questions and three programs on parameters, overloading, default arguments, recursion, lambda captures and static.
- Course: C++ study plan
- Module: Functions
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Functions is the checkpoint that closes the Functions module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: declarations and definitions, the three ways to pass a parameter and returning by value, overloading and default arguments, recursion and memoisation, lambdas and captures, and scope, lifetime and linkage.
How it works. Fourteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What is the difference between a declaration and a definition, and which error do you get when each is missing?
- When should a parameter be
T,const T&, andT&? - Given
f(int)andf(double), why isf(3L)a compile error? - Where may a default argument be written, and why must it be trailing?
- What two things must every recursive function have, and what happens without them?
- What is the difference between
[x]and[&x]in a capture list, and what doesmutablechange? - What does a function-local
staticdo on the second call, and what doesstaticmean at namespace scope?
The three programs are a recursive permutation generator, a grade report built from small const-reference functions with a default argument and an overload, and a leaderboard sorted with a lambda and numbered by a function-local static. Read each input format carefully; the functions named in the prompt are the contract.
Common questions
When should a parameter be T, const T& or T&?
T by value for small types such as int, double and pointers; const T& for anything larger that the function only reads, such as strings and vectors; and T& only when the function must modify the caller's object.
Given f(int) and f(double), why is f(3L) a compile error?
Converting long to int and long to double are both standard conversions of the same rank, so neither overload is better and the call is ambiguous. The compiler refuses to guess rather than pick one.
What does a function-local static do on the second call?
It keeps the value the first call left, because it is initialised only once and lives until the program exits. A static int counter = 0; returned as ++counter gives 1, then 2, then 3.
Exercises
Every arrangement
Read one word of 1 to 6 distinct lowercase letters. Write a recursive void permute(std::string& s, std::size_t k, std::vector<std::string>& out) that generates every arrangement by swapping each remaining character into position k, recursing on k + 1, and swapping back. Collect the results, sort them with std::sort, print one per line, then print count=<how many>.
Input: one word. Output: all permutations in lexicographic order, then the count line.
Example: input abc →
abc
acb
bac
bca
cab
cba
count=6Grade report
Read n, then n lines name m1 m2 m3 (marks 0–100). Write four small functions, prototypes above main:
double mean(const std::vector<int>& marks);std::string grade(double average);—Afor 90 and above,Bfor 80,Cfor 70,Dfor 60, otherwiseF(judged on the unrounded average).std::string grade(const std::vector<int>& marks);— an overload that grades the mean of the marks.std::string fmt(double value, int decimals = 1);— the value fixed todecimalsplaces, with the default on the prototype.
Print one line per student, <name>: <fmt(average)> <grade>, then class: <fmt(class average, 2)> <grade of the class average> where the class average is the mean of the student averages, and finally best: <name of the highest average — the first one on ties>.
Input: n, then n student lines. Output: n + 2 lines.
Example: input 2, Ada 95 90 90, Bob 70 75 72 →
Ada: 91.7 A
Bob: 72.3 C
class: 82.00 B
best: AdaLeaderboard with a lambda and a static rank
Read k passMark, then n, then n lines name score. Sort the entries with a lambda comparator: higher score first, and equal scores by name ascending. Write int nextRank() whose counter is a function-local static (first call returns 1) and std::string row(const std::string& name, int score) that returns <nextRank()>. <name> <score>. Print the first k rows (or all of them when there are fewer than k), then passed=<count> — the number of entries with score >= passMark, from std::count_if with a lambda that captures passMark.
Input: k passMark, n, then n entries. Output: up to k rows, then the passed= line.
Example: input 3 50, 5, zoe 70, al 90, bea 70, cid 40, dan 90 →
1. al 90
2. dan 90
3. bea 70
passed=4In this module: Functions
← Scope, lifetime and linkage · std::string — a value type for text →