7. Functions: reusable code
A function is defined with the def keyword, followed by a name, parentheses, and a colon. The indented lines below become the function body — the code that runs every time you call it.
Defining a function:
def greet():
print("Selam! Welcome to Almaz Café.")
This only defines the function — it does not run yet. To run it, you call it by name with parentheses:
greet() # output: Selam! Welcome to Almaz Café.
greet() # output: Selam! Welcome to Almaz Café.
greet() # output: Selam! Welcome to Almaz Café.
The function body runs every time you call it. You wrote the code once and reused it three times.
Function naming rules:
• Use lowercase letters and underscores (snake_case): greet, make_coffee, calculate_total
• No spaces — use underscore instead
• Names should describe what the function does
A function becomes much more useful when you can pass information into it. The variables inside the parentheses when you define the function are called parameters. The actual values you pass when you call the function are called arguments.
Adding a parameter — a personalised greeting:
def greet(name):
print("Selam,", name, "! Welcome to Almaz Café.")
greet("Abebe") # Selam, Abebe ! Welcome to Almaz Café.
greet("Sara") # Selam, Sara ! Welcome to Almaz Café.
greet("Almaz") # Selam, Almaz ! Welcome to Almaz Café.
You can have multiple parameters separated by commas:
def show_price(item, price):
print(item, "costs", price, "birr")
show_price("coffee", 25)
show_price("juice", 40)
show_price("water", 10)
Output:
coffee costs 25 birr
juice costs 40 birr
water costs 10 birr
The number of arguments you pass must match the number of parameters — otherwise Python raises a TypeError.
So far our functions only print things. But functions become even more powerful when they calculate a result and hand it back to the caller. The return statement sends a value back.
Example — calculate the total cost of an order including 15% service charge:
def total_with_service(amount):
service = amount * 0.15
total = amount + service
return total
bill = total_with_service(200)
print("Total bill:", bill, "birr") # Total bill: 230.0 birr
print(total_with_service(400)) # 460.0
The returned value can be stored in a variable, printed, or used in a calculation. The function stops running as soon as it hits return.
A function with no return statement returns None automatically.
Example — function that checks if a TeleBirr balance is enough:
def has_enough(balance, price):
if balance >= price:
return True
return False
print(has_enough(500, 300)) # True
print(has_enough(100, 300)) # False
You can give a parameter a default value by writing parameter=default in the definition. If the caller does not pass that argument, the default is used.
Example — a café receipt function where the service charge defaults to 10%:
def receipt(item, price, service_rate=0.10):
charge = price * service_rate
total = price + charge
print(item, ":", price, "+ service", charge, "= total", total, "birr")
receipt("coffee", 30) # uses default 10%
receipt("juice", 50, 0.15) # overrides to 15%
Output:
coffee : 30 + service 3.0 = total 33.0 birr
juice : 50 + service 7.5 = total 57.5 birr
Default parameters must come after non-default ones in the definition. Writing def f(x=1, y) is a SyntaxError in Python.
Scenario
Sara writes a function to convert Ethiopian Birr to USD: def birr_to_usd(birr): rate = 56.5 return birr / rate She wants to print the result for 1130 birr. Which line should she write?
Check your understanding
1/7 · 80 XPWhich keyword is used to define a function in Python?