Functions, Scope & Recursion
7. Functions, Scope & Recursion
7.1Reusable Blocks of Code (Functions)
Think of a microwave preset button — pressing "Popcorn" runs a saved set of cooking steps without you having to re-type the time and power every single time.
SHOW:
WHAT HAPPENED HERE
greet using def. Then we called it twice with different names, running the same code block with different inputs.THE TECHNICAL NAME
name) is a parameter.TAKEAWAY
def function_name(parameter):.7.2Returning Values
Think of an ATM machine — you enter your card and PIN, and the machine processes the request and hands back cash to you.
SHOW:
WHAT HAPPENED HERE
return keyword sent the calculated answer (40) back out of the function so we could store it in variable total.THE TECHNICAL NAME
TAKEAWAY
return to send a calculation back to your main program.7.3Variable Scope (Local vs Global)
Think of a bedroom light switch vs a house main breaker — the bedroom switch only controls lights inside that specific room.
SHOW:
WHAT HAPPENED HERE
tax_rate is accessible anywhere because it was created outside. total was created inside calculate_price and only exists inside that function.THE TECHNICAL NAME
TAKEAWAY
7.4Recursion (Functions Calling Themselves)
Think of Russian Matryoshka dolls — opening one doll reveals a smaller doll inside, until you reach the smallest solid doll that stops the sequence.
SHOW:
WHAT HAPPENED HERE
countdown printed 3 and then called countdown(2), which printed 2 and called countdown(1). When n <= 0, it printed "Blast off!" and stopped calling itself.THE TECHNICAL NAME
TAKEAWAY
GOTCHA 1
Forgetting a return statement when a function result is needed (functions implicitly return None).
GOTCHA 2
Forgetting a base case in a recursive function, causing a RecursionError (stack overflow).
GOTCHA 3
Attempting to modify a global variable inside a local scope without declaring it with global.
WHERE THIS SHOWS UP IN REAL CODE
Web APIs use functions to process user requests, while recursive algorithms traverse file directories and search nested category trees.
TRY THIS YOURSELF
Define a function double(n) returning n * 2 and call it with double(5).
Observe how recursive_factorial multiplies n by recursive_factorial(n - 1) until n <= 1.
Function Call Stack
Demonstrate function parameters, return values, variable scope, and recursive call stack.
Stretch Challenge: Explain Recursion & Calculate Fibonacci.
Steps:
- Print explanation:
Recursion is a function calling itself until hitting a base case.. - Define recursive
fib(n)(base case:if n <= 1 return n, recursive:fib(n-1) + fib(n-2)). - Call
fib(6)and printFib(6): 8.
What keyword is used to define a function in Python?
What happens if a Python function does not contain an explicit `return` statement?
What is a parameter in a function definition?
What is variable scope in Python?
Can code outside a function directly access a local variable defined inside that function?
What is recursion?
What is the purpose of a base case in a recursive function?
What error is raised if a recursive function lacks a base case?
In `def greet(name='Guest'):`, what is `'Guest'` called?
# comments · functions-recursion
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.