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.
// 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.
The parameter is a copy. Changing it leaves the caller’s variable untouched.
Guided simulation · Change the inputs to test your understanding.
With printRepeated(char symbol, int count = 3), what does printRepeated('#', 0) do?
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.
// 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
Mark complete when you can explain it yourself.