One function, one clear job
Follow a call from its arguments to its return value, and distinguish declarations from definitions.
A name for a reusable idea
A function packages one meaningful job: adding two values, checking a condition, or finding a position. Its contract says what inputs are valid, what it returns, and what it may change. A helper is easier to test when that contract is precise.
A definition describes the job. A call requests that the body run. A helper written above main does nothing until execution reaches a call to it. Moving repeated work into a function improves organization; it does not automatically reduce that work.
Parameters receive arguments
In int subtract(int first, int second), first and second are parameters. In subtract(9, 4), 9 and 4 are arguments. Order matters: the first call returns 5, while subtract(4, 9) returns -5. Each parameter needs its own type.
The caller pauses while the helper works, then continues with its result. Each ordinary call has fresh parameter values and local variables. Caller and helper variables do not need matching names.
// Complete C++23 program. No input; output: 5 9
#include <iostream>
int subtract(int first, int second) {
return first - second;
}
int twicePlusOne(int value) {
return 2 * value + 1;
}
int main() {
int answer = subtract(9, 4);
std::cout << answer << ' ' << twicePlusOne(4) << '\n';
}Declare before calling
The compiler needs the interface before it sees a call. Put the definition above main, or put a matching declaration such as int subtract(int, int); above main and the definition below it. A declaration ends with a semicolon; a definition supplies the body. A declaration alone does not implement the function.
A nested call such as add(add(2, 3), 4) needs the inner result before the outer call can use it. However, C++ does not promise left-to-right evaluation between separate argument expressions. Put state-changing argument computations in separate statements when order matters.
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 is defined above main but never called. How many times does its body run?
Turn the idea into code
Write larger(a, b), then reuse it in maximumOfThree(a, b, c). Both helpers must return a result without printing. Inputs are integers from −10⁹ to 10⁹. Test −8, −2, −5 and equal values.
Give me a hint
Compare a with b first, then compare that result with c.
Show the explained solution
The largest of the first two values is the only candidate from that pair that can win the final comparison. This uses constant time and constant auxiliary space.
// Complete C++23 program. Input: three integers.
#include <iostream>
int larger(int a, int b) { return a > b ? a : b; }
int maximumOfThree(int a, int b, int c) {
return larger(larger(a, b), c);
}
int main() {
int a = 0, b = 0, c = 0;
std::cin >> a >> b >> c;
std::cout << maximumOfThree(a, b, c) << '\n';
}Your notes
Mark complete when you can explain it yourself.