Chapter 4
Functions
Defining, calling, parameterizing, returning, testing, and documenting small reusable computations.
What this chapter does
Functions turn repeated logic into named, testable computation. This chapter teaches how inputs enter through parameters, values leave through return, and small tests keep behavior visible.
Lessons
Read these in order.
The chapter opening gives the main idea. Move through these lessons next; each page reuses ideas from the pages before it.
- 01Defining and Calling Functions
The difference between creating a function and running it.
- 02Parameters and Return Values
Arguments, parameters, return values, and why print is not return.
- 03Local Names
Names created inside functions and why explicit inputs are easier to test.
- 04Default Arguments
Supplying common parameter values while keeping function calls readable.
- 05Small Tests
Checking functions with known inputs, expected outputs, and simple assertions.
- 06Docstrings and Readable Code
Writing short function contracts with clear names and docstrings.
You are ready when
- Define and call functions.
- Pass arguments into parameters.
- Return values for later code to use.
- Explain local names inside a function.
- Use simple default arguments and keyword arguments.
- Write small tests and readable docstrings.
Where this leads
- Collections and Iteration
- Files, Paths, and Modules
- Small Numerical Experiments