Lessons 16–20 · About 60 min per lesson

Functions, argument passing and debugging

Split the program into steps and verify each function’s inputs and return value.

Lesson breakdown

  1. Lesson 16

    Write a function with parameters and a return value.

  2. Lesson 17

    Compare local variables with variables in the caller.

  3. Lesson 18

    Compare passing by value with a read-only const reference.

  4. Lesson 19

    Call an aggregation function directly with fixed inputs.

  5. Lesson 20

    Use a minimal failing test to locate an incorrect loop bound.

Worked example

Write count_passes, taking a list of scores and a pass mark.

How to solve it

Take a const vector<int>& and a threshold. Count scores at or above it. [59,60,75] returns 2 at threshold 60.

Assignment

Write sum_scores to return the sum of a vector and check it with three direct function calls.

Check your result

An empty vector gives 0, [4] gives 4, and [2,2,3] gives 7.