Python Foundations · Lesson 10 of 12

Subroutines and parameters

Defining functions, arguments and return values. Learn through short examples, practical tasks and targeted questions.

← All lessons

Writing reusable functions

Functions divide programs into manageable parts. Parameters accept values supplied by the caller; return sends a result back for use elsewhere. Compare printing a result with returning it—only the returned value can be directly used in another calculation.

Watch out: A function that prints a value does not automatically return it. In Python, functions without an explicit return produce None.

1 · Understand

Defining functions, arguments and return values. Study the code and predict what it does before running it. Change a value or string and observe how the output changes.

def rectangle_area(width, height):
    return width * height

print(rectangle_area(4, 3))
Download example .py ↓

2 · Practise

Write a function called is_even(number) that returns True for even values and False otherwise. Test several inputs.

Plan your inputs, processing and outputs. Write the code in Mu, IDLE or PyCharm; test at least three cases.

Show a reference solution after attempting the task
def is_even(number):
    return number % 2 == 0

for number in [2, 5, 12]:
    print(number, is_even(number))
Download reference solution .py ↓

3 · Debug the code

Identify the problem and explain how to fix it before revealing the answer.

def add(a, b):
    return a + b
print(add(2))
Reveal the corrected program and explanation
def add(a, b):
    return a + b
print(add(2, 3))

Compare the two snippets. Identify the changed line, run the corrected version and explain the effect.

4 · Check your understanding

Which keyword passes a result from a function to its caller?

5 · Challenge and reflect

Extension: Improve your solution to deal with unexpected input, add a second feature or display results more clearly.

Reflect: What was the hardest part? What would you test if someone else used your program?

Want extra practice? Try interactive Python tracing and debugging questions →

APPLY THE IDEA

Use this skill beyond the example

When you call a function, distinguish the argument sent by the caller from the parameter name used inside the function. Check whether the function prints a result or returns it; those actions are not interchangeable.

Quick check: Why can a function that only prints a result be hard to use in a calculation?

Check: Printing displays output but does not return the computed value to the caller.