Learning pathOriginal reference
Lesson 11 of 12

Optional: more expressive interfaces

Explore overloads, default arguments, and fixed-width matrix parameters after mastering ordinary calls.

Overloads differ by their parameter lists

Functions may share a name when their parameter lists distinguish them. An int larger(int, int) and a double larger(double, double) can serve two input types. Changing only the return type does not create a valid new overload with the same parameters.

Mixed arguments can be ambiguous: larger(4, 2.5) gives each of these candidates a different conversion to perform. Use appropriately typed arguments or a clearer interface rather than relying on a guessed selection.

Defaults fill omitted trailing arguments

void printRepeated(char symbol, int count = 3) lets a caller omit count. An explicit count always wins, including zero. The default is a supplied argument, not a minimum or a fallback for a false-like value.

For a separate declaration and definition, normally put the default only in the declaration visible to callers. Do not repeat the same default in the definition. Keep defaulted parameters trailing in these ordinary interfaces.

C++23
// Complete C++23 program. No input; output: ###, then a blank line.
#include <iostream>
void printRepeated(char symbol, int count = 3) {
    for (int i = 0; i < count; ++i) std::cout << symbol;
    std::cout << '\n';
}
int main() {
    printRepeated('#');
    printRepeated('#', 0);
}

A built-in matrix needs its row width

For a helper taking const int a[][3], the stored width of each row is part of the parameter type. Pass the active row count separately. A five-column array does not become compatible merely by changing a runtime variable.

Do not write int a[][] or use a runtime inner width as though variable-length built-in arrays were standard C++23. For a fixed width of three, visiting all active cells takes O(rows) time and O(1) helper auxiliary space. With independent positive dimensions r and c, a full scan is O(rc).

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

With printRepeated(char symbol, int count = 3), what does printRepeated('#', 0) do?

Choose an answer
Try it yourself

Turn the idea into code

Read 0 ≤ rows ≤ 100, then rows rows of exactly three integers in [−10⁹, 10⁹]. Write a read-only matrix helper and print the sum using a wide result.

Give me a hint

Share a constant column width between the caller’s array and the helper’s parameter. Use long long for the total.

Show the explained solution

The helper visits three cells per active row, so it has O(rows + 1) time and O(1) auxiliary space. The input wrapper allocates 100 × 3 integers regardless of the active row count.

C++23
// Complete C++23 program. Input: rows, then rows * 3 integers.
#include <iostream>
constexpr int COLUMNS = 3;
long long sumMatrix(const int a[][COLUMNS], int rows) {
    long long total = 0;
    for (int row = 0; row < rows; ++row) {
        for (int col = 0; col < COLUMNS; ++col) total += a[row][col];
    }
    return total;
}
int main() {
    int a[100][COLUMNS]{};
    int rows = 0;
    std::cin >> rows;
    for (int row = 0; row < rows; ++row) {
        for (int col = 0; col < COLUMNS; ++col) std::cin >> a[row][col];
    }
    std::cout << sumMatrix(a, rows) << '\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.