Learning pathOriginal reference
Lesson 1 of 12

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.

C++23
// 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.

Caller before5
Inside helper15
Caller after5

The parameter is a copy. Changing it leaves the caller’s variable untouched.

Guided simulation · Change the inputs to test your understanding.

Pause & predict

A helper is defined above main but never called. How many times does its body run?

Choose an answer
Try it yourself

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.

C++23
// 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

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.