Lesson 27/48 ยท โ๏ธ Functions
โ๏ธ FunctionsLesson 27/48
Phase 4 ยท Functions25 min
Default Parameters & Scope
Write more flexible functions and understand where variables live
Functions become much more powerful when you give parameters default values, so callers can omit arguments they don't care about. And understanding variable scope prevents some of the most confusing bugs beginners hit.
Default parameter valuespython
def greet(name, greeting="Hello", punctuation="!"):
return f"{greeting}, {name}{punctuation}"
print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Good morning")) # Good morning, Bob!
print(greet("Charlie", "Hey", ".")) # Hey, Charlie.Rule: Default parameters must come AFTER required ones.
def f(a=1, b) is a syntax error. def f(b, a=1) is correct.Variable Scope, where does a variable "live"?
Local scope: a variable created inside a function. It exists only while the function runs, then disappears. Global scope: a variable created outside all functions. It persists for the entire program.
Local variable does NOT change globalpython
score = 100 # global
def add_bonus():
score = 999 # this is a LOCAL variable, a different 'score'!
return score
print(add_bonus()) # 999 (local)
print(score) # 100 (global, unchanged)Avoid using
global to modify global variables from inside functions. It creates invisible dependencies that make code hard to follow and test. Instead: pass values in, return new values out.Pure functions: pass in, return outpython
# โ Bad, using global
total = 0
def add_to_total(x):
global total # modifies global, hard to track
total += x
# โ
Good, pure function
def add(current, x):
return current + x # returns new value, touches nothing global
total = 0
total = add(total, 5) # 5
total = add(total, 3) # 8
total = add(total, 10) # 18A pure function always produces the same output for the same input and has no side effects. Pure functions are easier to test, debug, and reuse, aim for them whenever possible.
Practice Exercises
0/3 solvedExercise 1 of 3easy
โฑ 00:00Flexible Greeting
Write a function
Examples:-
If
make_tag(content, tag="p", cls="") that wraps content in an HTML tag.Examples:-
make_tag("Hello") โ "<p>Hello</p>"- make_tag("Title", "h1") โ "<h1>Title</h1>"- make_tag("Warn", "div", "alert") โ "<div class=\"alert\">Warn</div>"If
cls is empty, don't include the class attribute.solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00Discount Calculator
Write a pure function
-
apply_discount(price, pct=10) that returns the price after applying a percentage discount.-
apply_discount(100) โ 90.0- apply_discount(200, 25) โ 150.0- apply_discount(50, 0) โ 50.0solution.py
2 / 3
Exercise 3 of 3medium
โฑ 00:00Pure Bank Account
Write three pure functions for a bank account, no global variables, just pass the balance in and return the new balance.
-
-
deposit(balance, amount) โ returns balance + amount- withdraw(balance, amount) โ returns new balance, but never below 0- get_status(balance) โ returns "rich" if โฅ 1000, "okay" if โฅ 100, "broke" otherwisesolution.py
3 / 3
Solve all 3 exercises to unlock completion