GCSE Python · Lesson 6 of 9

Functions, parameters and returns

OCR J277 2.2 / AQA 8525 programming

Use reusable functions to calculate average and assign a category.

← All GCSE Python lessons

Functions in larger programs

Separating input, calculation and display into functions makes a program easier to test. Use parameters to pass information and return values to share results with the caller. Identify which variables belong to each function.

Watch out: Avoid relying on global variables for every result. Passing data explicitly makes subroutines easier to reuse and debug.

1 · Learn and predict

Read this worked example. Predict its output, run it in Python 3, and explain how variables change.

def average(values):
    if len(values) == 0:
        return None
    return sum(values) / len(values)

print(average([4, 7, 10]))

2 · Build independently

Use reusable functions to calculate average and assign a category. Work from the starter code before comparing with the solution.

Download starter + solution ZIP ↓

For a stretch challenge: add meaningful test data, handle an edge case and explain one design decision.

Reveal reference solution
def average(values):
    if not values:
        return None
    total = 0
    for value in values:
        total += value
    return total / len(values)

def grade(mean):
    if mean is None:
        return 'No results'
    if mean >= 70:
        return 'High'
    if mean >= 50:
        return 'Secure'
    return 'Developing'

for values in ([60,80], [35,50], []):
    result = average(values)
    print(values, result, grade(result))

The reference is one possible approach. Test your own program before comparing.

3 · Debug and explain

Question: What is the difference between return and print in a function?

Show explanation

return supplies a value to the caller. print only displays text.

4 · Check your understanding

What does a parameter do?

5 · Reflect

Explain one test you used, one problem you solved and one improvement you would make.

This records completion only in this browser and does not certify mastery.

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

APPLY THE IDEA

Use this skill beyond the example

Choose a descriptive function name and one clear job for each function. Compare examples using parameters and return values with those relying on global state, and test the function independently.

Quick check: What is a benefit of returning a result?

Check: The caller can store or reuse the result in another expression or function.