Python Functions
A function gives one job a name. def pour(): writes that name down and keeps the indented line under it. It does not print. The print runs later, when you write pour(). Those parentheses are the call.
The parentheses after pour are empty, and a colon ends the line. The next line is indented four spaces, so it belongs to pour. Below that, pour() is written twice. Each of those lines is one call.
def pour():
print("tea")
pour()
pour()
The screen shows tea, then tea again. The first pour() runs the indented print once. The second pour() runs that same print once more. def itself writes nothing. A third pour() would write a third tea.
Parentheses make the call
pour() runs the indented line. The bare name pour does not. tea stays off the screen, and the program does not stop. The colon after the parentheses is required. The print line must be indented four spaces. def is lowercase. Def is a different word, and the program stops there.
A line level with def is outside the function. Python runs it when it reaches that line. The call is a separate step.
def pour():
print("tea")
print("ready")
pour()
The screen shows ready, then tea. print("ready") is level with def, so it runs as soon as Python reaches it. pour() is the next line, so tea comes second. The indented print still waits until that call.
Leave the call out
Keep def pour(): and keep print("ready"). Delete the line pour(). The screen shows only ready. print("tea") is still stored under the name, and nothing calls it. That finish is not an error.
What stops the program
Def with a capital D never starts a function. Leave out the colon after pour() and the program stops. If print("tea") begins in the same column as def, it is not inside the function. The definition then has no indented line, and the program stops. The bare name pour is the quiet case: the program can finish, and tea is never written.
pour always writes the same word, tea. It accepts no value. The next page is about giving a function a value to work with. This page does not send a value back from pour.
Read the screen first
- The first program writes pour() how many times, and tea how many times?
- The second program: what is the first line on the screen?
- You delete pour() and leave print("ready"). What is left on the screen?
- You write pour with no parentheses. Does the indented print run?
One reading
def pour(): stores the name and the indented print("tea"). That print runs only at pour(). Two calls write tea twice. print("ready") is level with def, so the second program writes ready and then tea. Delete the call, and tea is never written.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top