The Functions
All the notes. All the examples. All in one place. The challenges prove you understand it. Solve them all and you mastered functions.
▶📝 Cheatsheet
1. What is a Function

You make shaah every morning. Same steps: boil water, add tea, add sugar. Instead of repeating those steps every time, you write them once. That's a function. Write it once, use it whenever.
def make_shaah(): print("Boil water") print("Add tea") print("Add sugar") print("Pour")
make_shaah()make_shaah()Defining and calling
def greet(): print("Hello!")
greet()greet()greet()2. Parameters & Arguments
One parameter
def greet(name): print(f"Hello {name}!")
greet("Ahmed")greet("Faadumo")Multiple parameters
def order(item, qty): print(f"{qty}x {item}")
order("Pizza", 3)order("Coffee", 2)3. Return Values
Without return: the function does something but gives nothing back
def greet(name): print(f"Hello {name}")
greet("Ahmed")With return: the function gives a value back so you can use it
def double(number): return number * 2
result = double(5)print(result) # 10Using return in real code
def calculate_tax(price): return price * 0.10
tax = calculate_tax(200)total = 200 + taxprint(f"Tax: ${tax}") # Tax: $20.0print(f"Total: ${total}") # Total: $220.0Returning True or False
def is_adult(age): return age >= 18
print(is_adult(22)) # Trueprint(is_adult(15)) # False🧾 Reference Table
| Concept | Syntax | Example |
|---|---|---|
| Define a function | def name(): | def greet(): |
| Call a function | name() | greet() |
| One parameter | def name(param): | def greet(name): |
| Multiple parameters | def name(a, b): | def order(item, qty): |
| Return a value | return value | return price * 2 |
| Store a return | result = name() | result = double(5) |
▶🧩 Challenges
Challenge 1: Say Hello
Write a function say_hello() that prints "Hello, World!". Call it 3 times.
Hello, World!Hello, World!Hello, World!Challenge 2: Welcome Message
Write a function welcome(name) that prints "Welcome to Bashi Academy, {name}!". Call it with Ahmed, Hodan, and Farah.
Welcome to Bashi Academy, Ahmed!Welcome to Bashi Academy, Hodan!Welcome to Bashi Academy, Farah!Challenge 3: Full Name
Write a function full_name(first, last) that prints the full name in uppercase. Call it with 3 people.
AHMED YUSUFHODAN BILENIMO FARAHChallenge 4: Double It
Write a function double(number) that returns the number times 2. Print the result for 5, 10, and 25.
102050Challenge 5: Ticket Price
Write a function ticket_price(age) that returns the price. Under 12: $5, 12-17: $8, 18+: $12. Print the result for ages 10, 15, and 25.
Age 10: $5Age 15: $8Age 25: $12Challenge 6: Area Calculator
Write a function area(length, width) that returns the area. Area = length x width. Print the result for a room 5x4 and a room 10x3.
Room 1: 20Room 2: 30Challenge 7: Is Adult
Write a function is_adult(age) that returns True or False. Print the result for ages 12, 18, and 25.
12: False18: True25: TrueChallenge 8: Delivery Fee
Write a function delivery_fee(order_total) that returns the fee. Orders above $50: free. Under $50: $10 fee. Print the fee and total for orders of $35, $60, and $48.
Order: $35 - Fee: $10 - Total: $45Order: $60 - Fee: $0 - Total: $60Order: $48 - Fee: $10 - Total: $58Challenge 9: Greeting Card
Write a function greeting(name, message) that prints a formatted card. Call it with 2 different names and messages.
========================To: AhmedMessage: Happy Birthday!========================
========================To: HodanMessage: Congratulations!========================Challenge 10: Receipt Printer
Write three functions: subtotal(price, qty) returns the subtotal, tax(amount) returns 10% tax, print_receipt(item, price, qty) uses the other two to print a receipt.
======================== RECEIPT========================Item: PizzaQty: 3Subtotal: $30Tax: $3.0Total: $33.0========================