Functions and Decomposition

Decomposition is how a problem too large to hold in your head becomes a handful of steps you can name, write, and check one at a time. In Python those steps are functions: each one a contract with a single responsibility and clear expectations about its inputs, its return value, and its edge cases.

The same discipline is what lets the pieces fit back together. Returning a value instead of printing keeps a function usable by a test or by the next stage of a pipeline; local names and no shared global state keep one piece from quietly breaking another. Docstrings, type hints, and a little input validation record what each unit promises — so a program assembled from small, isolated parts stays something you can read, test, and change.

Figure: Infographic about Functions

Listen

Overview of Session
How Decomposition Cures Project Paralysis
Python Functions as Legally Binding Contracts

Presentation

  • Decomposition - Breaking a large problem into smaller pieces: tasks, steps, and the four payoffs of clarity, reuse, checking, and collaboration
  • Functions as Decomposition - How the four payoffs of decomposition become Python function design

Read

Hands-on

Notebooks in 04-Functions-Decomposition