Free course · Lesson 3 of 5
Functions & Scope
In this lesson you will
- Define a function with parameters and a return value
- Reuse one function for different inputs
A function packages a piece of logic under a name so you can reuse it instead of
copy-pasting code. Python functions can take parameters, return a value with
return, and even provide sensible defaults.
def greet(name):defines a function;greet("Grace")calls it.- Default parameters:
def greet(name="friend"):makes an argument optional. - Variables created inside a function are local — they don't leak outside it.
- Good function names and single responsibilities make code easier to test and debug.
💡 Practical Skill: Break a big task (like generating an invoice) into small,
named functions — calculate_total(), apply_discount(),
format_receipt() — each easy to test on its own.
def calculate_total(price, quantity, discount_percent=0): subtotal = price * quantity discount = subtotal * (discount_percent / 100) return subtotal - discount # Reuse the same function for different ordersorder_1 = calculate_total(price=2500, quantity=3)order_2 = calculate_total(price=2500, quantity=10, discount_percent=15) print(f"Order 1 total: {order_1} SSP")print(f"Order 2 total (bulk discount): {order_2} SSP")
Practice exercise
Write convert_to_usd(amount_ssp, rate) and use it for three amounts.
Key takeaways
- return hands a value back; print only displays it
- Default arguments make parameters optional
- Good names describe what a function does
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.