Programming Fundamentals Using C++

L15 · Function Overloading, Default Arguments, and Reference Parameters

Module 8 — Advanced Function Concepts · Week 8 · Lecture 15 of 32 · 120 minutes Outcomes: CLO-5 · PF-8.1, PF-8.2 · LEARNING_OUTCOMES.md

Learning objectives

  1. Create valid overloaded function sets (distinct parameter lists), predict which overload a call selects, and recognize ambiguity errors (PF-8.1).
  2. Write functions with default arguments and state the two rules (defaults rightmost; declaration-only) (PF-8.1).
  3. Implement reference parameters (T&, const T&) for genuine multiple-output patterns (swap, min/max pair, statistics bundle) and justify value vs const T& vs T& for a given parameter (PF-8.2).

Prerequisites

L13–L14 (copy semantics, scope); L07 (booleans for overload selection examples).

Concept sequence

  1. One job, many shapes: why overloading exists
  2. Overload resolution: the compiler's matching rules
  3. Ambiguity: when the compiler refuses to choose
  4. Default arguments: rightmost rule, declaration placement
  5. References as parameters: the alias model
  6. The output-parameter pattern + const T& for big inputs

Teaching topics (detailed)

C++ examples required

FileRole
overload_ref_demo.cpp ✅overloaded print set with resolution prints; working swap; min-max output pattern; an intentionally ambiguous call (commented)
(live) stats_outparams.cppstatistics function with 3 output references, called with pre-declared result variables

Common student misconceptions

Conceptual explanation (beginner-first)

Week 7's functions processed a row of numbers. But much data is a table: rows of students, columns of test scores; a chessboard; an image's pixels. A two-dimensional array is literally "an array of arrays" — int grid[3][4] is 3 rows, each with 4 columns, stored row by row in memory. We process tables with nested loops from week 5: the outer loop steps through rows, the inner loop through columns.

Two new skills matter here. First, parameter passing: a 2D array parameter must declare the column count (int g[][4]), because the compiler needs to know how wide each row is to compute addresses — the row count stays free. Second, aggregation: the most common real jobs are totals per row, totals per column, and grand totals — each is a small loop pattern placed at the right nesting level.

Terminology and definitions

TermDefinition
Two-dimensional arrayT name[rows][cols] — a table of rows × columns elements
Row-major storageRows stored one after another in memory
2D array parameterT g[][cols] — column count required, row count free
Row sum / column sumTotal across one row / down one column
Grand totalSum over all elements
AggregationReducing many values to one (sum, max, count)
Indexingg[row][col] — row first, then column

Syntax and C++ examples

const int ROWS{3};
const int COLS{4};

int grid[ROWS][COLS] =
{
    { 1,  2,  3,  4},
    { 5,  6,  7,  8},
    { 9, 10, 11, 12}
};

// column count mandatory; row count free — the compiler needs the width
long long rowSum(const int g[][COLS], int rows, int r)
{
    long long total{0};
    for (int c{0}; c < COLS; ++c)
        total += g[r][c];
    return total;
}

// grand total — nested loops, the week-5 pattern returns
long long grandTotal(const int g[][COLS], int rows)
{
    long long total{0};
    for (int r{0}; r < rows; ++r)
        for (int c{0}; c < COLS; ++c)
            total += g[r][c];
    return total;
}

Line-by-line code explanation

examples/matrix_ops.cpp:

  1. The matrix is initialized with a brace list of brace lists — each inner list is one row.
  2. printMatrix(const int m[][COLS], int rows) — outer loop rows, inner loop columns, \t between cells, \n after each row: the placement rules from the triangle exercise, now printing data instead of stars.
  3. rowSums(...) fills an output 1D array: for each row, sums across, stores into sums[r]. A 2D-in, 1D-out function — the workhorse shape of data processing.
  4. transpose(...) writes dst[c][r] = src[r][c] — swapping the indices mirrors the table across its diagonal.

Output prediction questions (with answers)

  1. int a[2][3]{{1,2,3},{4,5,6}}; — what is a[1][0]? — 4.
  2. For that a, rowSum(a, 2, 0) — ? — 6.
  3. Grand total of a — ? — 21.
  4. After transpose, the element at [0][2] came from — ? — [2][0] of the original.
  5. A function declares int g[3][] (rows fixed, columns blank) — why does it not compile? — the compiler needs the column stride to compute element addresses; rows are the free dimension.

Common errors and debugging examples

ErrorSymptomFix
int g[3][] in a parameterCompile error: missing column boundint g[][COLS]
Swapped [row][col]Data lands in the wrong cellSay "row first" aloud while writing
<= in a row/col loopReads past the row into the nextc < COLS, r < rows
Row sum inside the inner loopResets/accumulates wronglyAccumulate per row, store after inner loop
Flat initializer mismatchZeros fill the gaps silentlyBrace one inner list per row

Classroom demonstrations

  1. Spreadsheet on the board: draw the 3×4 grid with indices on both axes; students call out g[r][c] values as you point.
  2. Row-major reveal: print the six bytes of a small char grid with addresses — memory shows the rows lying end to end.
  3. Transpose theater: write the matrix on paper, fold along the diagonal — the indices swap visually.

Guided student activities

  1. Row/column sum trio: in pairs, write rowSum, colSum, and grandTotal; test all three on a 2×2 matrix where the answers are obvious.
  2. Largest per row: extend rowSums to rowMaxes — seed with the row's first element (the L11 lesson, reused).
  3. Trace the transpose: hand-trace transpose on a 2×3 input; the output is 3×2 — verify against the program.

Practice problems

Summary

A 2D array is an array of arrays: g[r][c], row-major in memory, and as a parameter it needs int g[][COLS] — columns fixed, rows free. Nested loops walk the table; totals per row, per column, and overall are the same aggregation patterns from week 5 wearing table clothes. Next (L16): the midterm — everything through two-dimensional arrays.

Exit ticket / formative assessment

  1. Declare a 2×5 table of double named rates and initialize both rows explicitly.
  2. Write the full header of colSum(const int g[][COLS], int rows, int c) returning long long.
  3. Why must the column count appear in a 2D array parameter but not the row count?
  1. print('A') with print(int) and print(double) overloads — which runs?
  2. Two rules for default arguments?
  3. Mark each as value / const T& / T&: (a) 1000-element vector to search; (b) result collector; (c) small int input.

Estimated time allocation (120 min)

SegmentMinutes
Recall (scope quiz) + why overloading10
Overload resolution + ambiguity ladder30
Break10
Default args + reference parameters + output patterns40
Resolution court activity20
Exit ticket + midterm preview10
Programming Fundamentals Using C++ · C++17 · 16 weeksBack to top ↑