Defining a Function
A function definition uses the def keyword, a function name, parentheses, a colon, and an indented block of statements.
Instructions for Later
A function definition creates a reusable block of code, but creating that block does not immediately run the instructions inside it. When Python encounters a definition, it stores the instructions under the function's name. The instructions run later, when the function is called. Keeping these two actions separate is essential for understanding why a program may define a function without producing output immediately.
What do you think happens?
What happens when Python reads this definition before any call appears?
Reveal answer
Answer: The function is stored for later use
Defining a function stores it in memory but does not execute the code inside. A function call is what executes the body.
Definition Line Anatomy
A Python function definition begins with the def keyword. The function name follows it, then a pair of parentheses, and then a colon. The colon marks the end of the definition line and signals that an indented block of statements follows. Together, these parts tell Python that the following indented statements belong to a named function.
The parentheses are part of the function-definition structure even when no parameters are shown. The indented block is also part of the structure: it contains the statements that Python will run when the function is called.
Definition and Call
| Action | What Python does | When the body runs |
|---|---|---|
| Function definition | Stores the function and its instructions in memory | Not during the definition |
| Function call | Jumps to the function definition and runs its body | During the call |
A definition and a call have different jobs. In a definition, the name is introduced and the indented instructions are stored for later use. In a call, the function's name is used to request execution. Python then runs the stored body and returns to the line after the call.
def greet(): print("Hello") greet()
Execution Path
The control flow has a clear order. First, Python encounters the definition and stores the function. It then continues to the next statement instead of entering the body. When Python later reaches a call, it jumps to the stored definition, runs the body, and then returns to the line after the call. The definition is therefore encountered before the call, while the body is executed only during the call.
Tracing a Greeting
Determine when the message is displayed in this sequence: a function definition followed by a function call.
Read the definition: Python stores the function named greet and its indented print instruction. The print instruction does not run yet.
Reach the call: Python reaches greet(), which requests execution of the stored function.
Run the body: Python runs the print instruction inside the function body.
Continue: After the body finishes, control returns to the line after the call.
The message is displayed when greet() is called, not when greet is defined.
Indentation Boundaries
The colon at the end of the definition line signals that an indented block follows. Statements in that indented block belong to the function body. The body is the set of instructions Python will run when the function is called. Indentation therefore shows which statements are inside the function rather than outside it.
Mistakes in Definitions
Forgetting the colon after the parentheses
The colon marks the end of the definition line and signals that an indented block follows.
Fix:
def greet(): print("Hello")Not indenting the function body
The statements that belong to the function must form an indented block after the colon.
Fix:
def greet(): print("Hello")Defining a function without calling it
The definition stores the instructions but does not execute the body.
Fix:
def greet(): print("Hello") greet()Calling a function before defining it
A function call must refer to the function after it has been defined and stored.
Fix:
def greet(): print("Hello") greet()
When checking a function definition, inspect the structure from left to right: confirm def, confirm the function name, confirm the parentheses, confirm the colon, and then confirm that the body is indented. Finally, check whether the function is called after its definition.
Practice the Trace
Write a function definition named welcome with an indented statement that displays a message, then add a call so that the body executes. As you write it, label the definition, the body, and the call in your own notes.
Hints
- Start the definition with def.
- Place parentheses and a colon after the function name.
- Indent the statement that belongs to the body.
- Place the call after the definition.
A function is defined correctly, but nothing from its body appears to happen. What should you check first, and why?
Hints
- Look for a function call.
- Remember that defining stores the instructions, while calling executes them.
Key Takeaways
- A function definition uses def, a function name, parentheses, a colon, and an indented block.
- Defining a function stores its instructions but does not execute the body.
- Calling a function runs the body and then returns control to the line after the call.
- The colon signals the indented function body, and indentation determines which statements belong to it.
- Common mistakes include forgetting the colon, failing to indent the body, defining without calling, and calling before defining.
Key Takeaways
- A function definition creates a named, reusable block of instructions.
- The def keyword, function name, parentheses, colon, and indented body form the basic definition structure.
- Python stores a function when it reads the definition and executes its body only when the function is called.
- After the body finishes, control returns to the statement after the call.
- Checking syntax, indentation, definition order, and the presence of a call helps correct common mistakes.