Skip to content
Back to course

7. Functions: reusable code

Imagine Almaz runs a small café in Addis Ababa. Every morning she follows the same steps to make a cup of coffee: grind beans, boil water, pour, serve. Instead of explaining these steps from scratch every time a customer orders, she just says "make a coffee." Functions in Python work the same way — you write the steps once, give them a name, and then call that name whenever you need the job done. In this lesson you will learn to define functions, pass information into them, and get results back from them.

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.

People often use "parameter" and "argument" interchangeably, and that is fine in casual conversation. Technically: a parameter is the variable name in the function definition (def greet(name)), while an argument is the actual value passed when calling the function (greet("Abebe")). Both words refer to the information going into the function.

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?

Lesson recap: • A function lets you write a block of code once and reuse it many times. • Define with def name(): — then indent the function body. • Call a function by writing its name followed by parentheses: name() • Parameters are the variables in the definition; arguments are the values passed when calling. • return sends a value back to the caller; a function without return gives back None. • Default parameter values (def f(x, y=10)) make some arguments optional. • Functions can call other functions — break big problems into small steps. • Good function names are lowercase with underscores and describe the action (make_coffee, calculate_total).

Check your understanding

1/7 · 80 XP

Which keyword is used to define a function in Python?