Chapter 4

Functions

Defining, calling, parameterizing, returning, testing, and documenting small reusable computations.

Subject
Python
Lessons
6 lessons
Practice
11 checks
Review
3 pages

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.

  1. 01
    Defining and Calling Functions

    The difference between creating a function and running it.

  2. 02
    Parameters and Return Values

    Arguments, parameters, return values, and why print is not return.

  3. 03
    Local Names

    Names created inside functions and why explicit inputs are easier to test.

  4. 04
    Default Arguments

    Supplying common parameter values while keeping function calls readable.

  5. 05
    Small Tests

    Checking functions with known inputs, expected outputs, and simple assertions.

  6. 06
    Docstrings and Readable Code

    Writing short function contracts with clear names and docstrings.

  1. Conclusion

    What functions prepare before collections and iteration.

  2. Review

    A compact review of definitions, calls, parameters, return values, local names, defaults, tests, and docstrings.

  3. Exercises

    Chapter-level practice for calling, returning, testing, and reading small functions.

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

Chapter progress