Lesson 11: Decomposing a program into functions
We already know how to define functions and call them — now we learn the craft: how to decompose a whole program into the right functions. The central rule: one function = one job, with a name that says exactly what it does. We'll build a small calculator with add, sub and mul, a print_menu function
A good program is like a restaurant kitchen: a salads station, a grill station and a desserts station — and a head chef who only routes orders to the right station. No station does everything, so everything runs smoothly.
- decomposition
- Splitting a large program into small functions, each with one clear job.
- one job
- The principle that each function does exactly one thing — easy to test, fix and reuse.
- main as conductor
- A thin main that doesn't compute by itself: it calls the right functions in the right order.
- dispatch
- Choosing which function runs based on user input, e.g. with an if / else if chain.
- top-down design
- Start from the big task, break it into sub-tasks, and only then write each function.