Everything else is ready: study the parts, the key terms and the quiz below.
- Part 11The same lines, twice
- Part 22def, and calling a function
- Part 33What a call does
- Part 44return, print and None
- Part 55return ends the function
- Part 66Returning several values
- Part 77Docstrings and type hints
- Part 88Built-in and user-defined, and the first mistakes
- Part 99Recap, your challenge, and next
Defining Functions
Package code into reusable functions with inputs and outputs.
In this lesson9 parts
- 01The same lines, twice
- 02def, and calling a function
- 03What a call does
- 04return, print and None
- 05return ends the function
- 06Returning several values
- 07Docstrings and type hints
- 08Built-in and user-defined, and the first mistakes
- 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.50inwith_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
defstatement; 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:
priceindef with_tax(price):. - return value
- The value a call gives back. A function with no
returngives backNone. - 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.