Return, print, and start fresh
Use return values, void actions, early exits, and local scope deliberately.
Returning is different from printing
return value; sends a value back to the caller. std::cout writes output. A call that returns 12 does not display 12 unless a statement explicitly prints it. A caller can store a result, compare it, or use it in another calculation.
A return exits the current function immediately, even when it appears inside a loop. break exits only the innermost loop or switch, while continue starts the next loop iteration. Every reachable result-producing path of an ordinary non-void helper must return a value.
// Complete C++23 program. Input: two integers in [-10^9, 10^9].
#include <iomanip>
#include <iostream>
double average(int a, int b) {
long long total = 1LL * a + b;
return static_cast<double>(total) / 2.0;
}
int main() {
int a = 0, b = 0;
std::cin >> a >> b;
std::cout << std::fixed << std::setprecision(1)
<< average(a, b) << '\n';
}Actions can return void
void means there is no result value to store. A helper named printLine may print deliberately; a helper named isEven should normally return a bool so the caller chooses what to do with the decision. A void helper can leave early with return; or reach its closing brace.
An empty parameter list is valid. void printWelcome() needs no arguments, while int readChoice() could take no arguments and still return a value. Parameter count and return type are separate decisions.
Scope and lifetime are different
Scope describes where a name can be used. A counter declared in a for-loop header belongs to that loop. A variable named total inside a helper is separate from a variable named total in main.
Lifetime describes when an object exists. An ordinary local total initialized to zero starts again on each call; it does not remember the previous answer. Return a result by value when needed. Returning a reference to a destroyed local object cannot keep it alive.
// Complete C++23 program. No input; output: 6 6 100
#include <iostream>
int sumTo(int n) { // Contract: 0 <= n <= 1000.
int total = 0;
for (int i = 1; i <= n; ++i) total += i;
return total;
}
int main() {
int total = 100;
std::cout << sumTo(3) << ' ' << sumTo(3) << ' ' << total << '\n';
}Value or reference?
The helper executes x += 10. Compare the caller’s value before and after the call.
The parameter is a copy. Changing it leaves the caller’s variable untouched.
Guided simulation · Change the inputs to test your understanding.
A helper returns 12. main executes calculate(); and discards the result. What must be printed?
Turn the idea into code
Implement clampToRange(value, low, high) for low ≤ high. Return the nearest endpoint for an outside value; otherwise return the original value. Test 17 3 10, both endpoints, and a negative interval.
Give me a hint
Use two early returns for values outside the range; the remaining path is already inside.
Show the explained solution
The three cases cover all permitted inputs. Each performs a fixed number of comparisons, so the helper uses O(1) time and auxiliary space.
// Complete C++23 program. Input: value low high, with low <= high.
#include <iostream>
int clampToRange(int value, int low, int high) {
if (value < low) return low;
if (value > high) return high;
return value;
}
int main() {
int value = 0, low = 0, high = 0;
std::cin >> value >> low >> high;
std::cout << clampToRange(value, low, high) << '\n';
}Your notes
Mark complete when you can explain it yourself.