Lecture 14 — Functions and Reuse

Position in the course

This is lecture 14 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 04–13 — any core construct

Assumed fluencies:

Preparation task: Find one repeated block in your own homework and name it.

Detailed topic outline

Core (mandatory):

  1. functions as named subproblems
  2. parameters, returns, and scope
  3. refactoring a monolith
  4. mutable default arguments

Extension (optional enrichment):

  1. composition: pipelines of functions
  2. interfaces before implementation

Case sequence

CaseTitleDifficulty
cs-053The Validator TrioBeginner
cs-054Refactor the MonolithFoundational
cs-138The Downtime LedgerFoundational
cs-055The Parameter JungleIntermediate
cs-056Pipelines as FunctionsExpert

Display each case full-screen from the student page (cases cs-053–cs-056); instructor pages cs-053–cs-056 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts. Applied additions cs-138 extend the session as optional extra attempts or homework; they share the lecture's theme but teach an applied, real-world angle.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-053): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-054): same rhythm
50–70Case 3 (cs-055): same rhythm, harder case
70–75Break
75–100Case 4 (cs-056): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Refactor the validator trio (cs-053) live: extract one function, rename, re-run the same tests, and show nothing broke.

Classroom activities

#ActivityMinutesFormat and prompt
1Refactor kata10Pairs extract one function from a provided monolith; two solutions are compared on naming and signature.
2Interface sketch6Before any implementation, teams write only the signatures and docstrings for cs-056's pipeline.
3Default trap discussion4The mutable-default classic is shown; predict, run, explain, and state the fix.

Board plan

Instructor explanation notes

Reading the room. Functions are taught as named subproblems first and syntax second: if the name is wrong, the signature is wrong, and the code is wrong. Refactoring the monolith (cs-054) is the emotional peak — same behaviour, half the mess.

Running the reveal. Mutable defaults are demonstrated, not lectured; the trap is more convincing when it bites live.

Lecture focus. One sentence to repeat verbatim: Name the subproblem before you write the function.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. Extract two single-purpose functions from a provided monolith; behaviour must not change.
  2. Design signatures first for the cs-056 pipeline; then implement the smallest one.
  3. Fix the mutable-default trap in a provided accumulator function and explain the mechanism.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. What makes a function name good? Expected: it states the subproblem's contract: verb + object, no surprises
  1. Why are mutable defaults dangerous? Expected: the default object is created once and shared across calls
  1. What does 'refactor without behaviour change' demand? Expected: the same tests pass before and after, unchanged

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation