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 + tax
print(f"Tax: ${tax}") # Tax: $20.0
print(f"Total: ${total}") # Total: $220.0Returning True or False
def is_adult(age):
return age >= 18
print(is_adult(22)) # True
print(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 YUSUF
HODAN BILE
NIMO FARAHChallenge 4: Double It
Write a function double(number) that returns the number times 2. Print the result for 5, 10, and 25.
10
20
50Challenge 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: $5
Age 15: $8
Age 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: 20
Room 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: False
18: True
25: 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: $45
Order: $60 - Fee: $0 - Total: $60
Order: $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: Ahmed
Message: Happy Birthday!
========================
========================
To: Hodan
Message: 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: Pizza
Qty: 3
Subtotal: $30
Tax: $3.0
Total: $33.0
========================