Returning Values

intermediate25 min

Learning objectives

  • Distinguish between procedures and value-returning functions
  • Use the return statement correctly
  • Reuse returned values within larger programs

Learn

AQA 4.1.1.12 — Returning values

Retrieval: the previous lesson's grade_student(name, score) took parameters but only ever printed a result — it never gave the rest of the program anything to work with afterwards. This lesson fixes that limitation.

A procedure performs an action; a function performs an action and hands a value back to whoever called it, using return. Once a function returns a value, the caller can store it, test it, or pass it straight into something else — a printed value can't be reused, but a returned one can.

def calculate_grade(score):
    if score >= 70:
        return "A"
    elif score >= 60:
        return "B"
    else:
        return "C or below"

grade = calculate_grade(82)   # the returned value is captured in "grade"
print(f"Result: {grade}")
print(grade == "A")            # the returned value can be tested, compared, reused

Common mistake

Calling a function and using its result are two separate steps — calculate_grade(82) on its own does nothing useful; you must assign or use the returned value: grade = calculate_grade(82). A closely related mistake: if a function has no return statement on some path (or none at all), Python doesn't raise an error — it silently returns None, which then causes confusing failures later when your code tries to use that None as if it were a real result.

def broken_grade(score):
    if score >= 70:
        return "A"
    # no return for scores below 70 - falls through and returns None!

result = broken_grade(55)
print(result)   # None - not an error, just quietly wrong

Worked example — a grade calculator that returns a classification

def classify(percentage):
    if percentage >= 90:
        return "Distinction"
    elif percentage >= 70:
        return "Merit"
    elif percentage >= 40:
        return "Pass"
    else:
        return "Fail"

student_percentage = float(input("Enter percentage: "))
classification = classify(student_percentage)
print(f"You achieved: {classification}")

Because classify returns rather than prints, the same function could be reused inside a larger program — for example, to classify every student in a class and count how many achieved each grade — without changing a single line of classify itself.

Trace it — find the bug

def double(n):
    result = n * 2   # calculated, but never returned!

value = double(5)
print(value + 1)

Trace this by hand: what does double(5) actually return, and what does the final print line output? (Answer: double has no return statement, so it returns None regardless of n — the final line then crashes trying to add 1 to None.)

Challenge

Extend the classify function so it also returns a numeric grade point (e.g. Distinction = 4, Merit = 3, Pass = 2, Fail = 0) alongside the classification, using a tuple: return classification, grade_point. Then write code that calls it for three different students and prints their total grade points added together — this only works because the values were returned, not printed.

Looking ahead: the next lesson (Local Variables) explains exactly why a variable created inside classify — like percentage or classification — disappears the moment the function finishes, which is precisely why return is the only way to get a value out.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab

Test yourself

Check your understanding with exam-style questions.

Go to Exam Practice
Log in to track this lesson on your progress dashboard.
Log in