Python 4.1Code walkthrough9 parts

Defining Functions

Package code into reusable functions with inputs and outputs.

Loading your progress…Next: 4.2 Arguments and Scope

In this lesson9 parts

  1. 01The same lines, twice
  2. 02def, and calling a function
  3. 03What a call does
  4. 04return, print and None
  5. 05return ends the function
  6. 06Returning several values
  7. 07Docstrings and type hints
  8. 08Built-in and user-defined, and the first mistakes
  9. 09Recap, your challenge, and next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

argument
A value passed to a function in a call: 12.50 in with_tax(12.50).
call
Running a function's body, by writing its name and round brackets: with_tax(12.50).
docstring
A string on the first line of a function's body that says what the function does. help() and __doc__ show it.
frame
The namespace one call runs in. It holds the parameters and the names the body binds, and is removed when the call returns.
function definition
A def statement; running it creates a function object and binds its name. The body does not run until a call.
parameter
The name in a function's definition that a value will be bound to: price in def with_tax(price):.
return value
The value a call gives back. A function with no return gives back None.
type hint
A note in a function's header of the type a parameter should have, or the type it returns: review: str, -> list[str]. Python does not check it when the code runs.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 1 · Functions and Arguments.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.