Functions — Breaking Problems into Machines

← Course home · ← Iteration module · Unit 07–08/16 · Syllabus

You can now write programs of any shape: decisions to choose, loops to repeat. But everything so far has lived in one main() — and the mini-project showed what happens at scale: eighty lines of copy-pasted input readers, five accumulators in one scope, and no way to test one piece alone. Functions fix all three. This is the unit where programs stop growing longer and start growing structured.

In this module you will learn

Module map

Page What’s inside
Lesson 1 — The function machine Declaration/definition/call, parameters vs arguments, return values, void, visual call flow, trace tables
Lesson 2 — Copies, scope, prototypes Pass-by-value, local/global variables, function prototypes, decomposition as a method
Lesson 3 — References & testing & parameters that write back, const& for big inputs, per-function test tables, reusable code
Lesson 4 — Overloading, defaults, refactoring Same name, different signatures; default arguments; a full refactor walkthrough; the common errors gallery
Exercises 26 exercises with separated solutions (S1–S26)
Debugging 10 seeded function bugs — find/fix/reflect
Refactoring 10 before/after refactoring exercises
Challenges 10 challenges with separated solutions
Labs 8 multi-function design labs
Mini-project The Menu-Driven Utility Toolkit, built progressively with functions

Try it yourself first

The same rule as every module: attempt 15 minutes before opening any solution. Here it has an extra edge — function design has no single right answer, so comparing your decomposition with the solution’s is where the learning lives. Ask of any two designs: which names read better, which has fewer parameters, which can be tested alone?

Pacing

Units 07–08 span two weeks (4 sessions):

What comes next

The arrays module is next — and the moment you have a collection, functions that take collections as parameters make every pass testable one function at a time. Everything in the Marks Analyzer is built as a function team.

Checklist