Overview
Functions encapsulate a block of reusable code; call them by name with arguments.
Analogy
A recipe: define it once, use it many times with different ingredients.
Step-by-step
- def greet(name): defines the function.
- return sends a value back to the caller.
- Call it: result = greet('Alice').
- Default parameters: def greet(name='World'):
Visual
def → [body stored] → call → args bound → body executes → return value
Common mistakes
- Forgetting return; the function returns None implicitly.
- Shadowing a built-in (def list(): ...)
Practice questions
- Write a function to compute the area of a circle.
- Write a function with default and keyword arguments.