Skip to content
Topos Uranos

Summary

This lesson opens the fifth unit of the course, devoted to functions. It explains what the unit is for: to teach how to decompose a problem into named parts, each with an interface and a contract, and how to solve certain problems by reducing them to a smaller instance of themselves, by recursion; it presents its four lessons and the thread that joins them; it recalls what the unit takes for granted, above all the loops of unit 4 and their invariants; it formulates the problem the unit solves, that of a program that repeats the same copied computation and that, because of a slip in the copy, answers wrongly without the compiler saying anything; it anticipates the contract (the precondition and the postcondition) as the form in which the unit will specify each part; and it shows where it leads, in the following units and in the course Programming in C++ II.

Learning objectives

  1. Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
  2. Recall the algorithms of unit 4 (the trace of a loop, its invariant and its variant) and review them if necessary.
  3. Anticipate, with an example, the idea of a contract: what a part of the program demands of whoever uses it and what it guarantees in return.
  4. Recognize what one is expected to be able to do at the end of the unit, and distinguish it from what will be learned in the following units.

Entry assessment

Before working through the unit, it is worth measuring the starting point. This assessment is a self-assessment: it is not graded, and its only purpose is to guide your study. The first five questions check what the unit takes for granted: the trace of a loop, its invariant, the precondition of a specification, the copying of a value in an initialization and the search, within a program of unit 4, for a computation with its own inputs and outputs. The next five touch, in the order of the lessons, on the central ideas of the unit: the call stack, the passing of an argument, the separation between computation and presentation, the recursive definition of the factorial and the execution of a function that calls itself. No one is obliged to get them right yet, and in some of them “I don’t know yet” is an honest answer: each answer, right or not, explains the point and names the lesson that deals with it. The same skills are measured again at the end, in the synthesis lesson.

Entry assessment

Before starting the unit, measure your starting point. It is a short self-assessment: it is not graded and its result is stored only in your browser.

  1. Question 1From earlier unitsWhat does int n{305}; int c{1}; while (n >= 10) { n = n / 10; ++c; } std::println("{}", c); write?

  2. Question 2From earlier unitsIn the loop int total{0}; int k{1}; while (k <= n) { total += k; ++k; }, with n≥0n \geq 0, what relation holds each time the condition is evaluated?

  3. Question 3From earlier unitsThe specification of division says: “given integers a≥0a \geq 0 and b>0b > 0, obtain qq and rr with a=b⋅q+ra = b \cdot q + r and 0≤r<b0 \leq r < b”. What is its precondition?

  4. Question 4From earlier unitsAfter int a{3}; int b{a}; b = 7;, what is the value of a?

  5. Question 5From earlier unitsThe program of lesson 4.4 that writes the primes less than 100 has a loop that runs through the candidates and, inside it, another that tries their divisors. Which part is a computation with its own inputs and outputs, one that would be worth naming?

  6. Question 6A program starts in main; main calls a function g, and g calls another function h. While h is running, which calls are in progress?

  7. Question 7With the function int f(int x) { x = x + 1; return x; }, what does int a{3}; int b{f(a)}; std::println("{} {}", a, b); write?

  8. Question 8A program reads a date and must write its day of the week. Which decomposition separates the computation from the presentation?

  9. Question 9Which of the following definitions defines n!n! for every natural number nn?

  10. Question 10With int f(int n) { if (n == 0) { return 0; } return n + f(n - 1); }, what does std::println("{}", f(3)); write?