lesson 3.8.5 getting answers with functions
A fun, complete lesson plan for KS3/4 Computing on using the 'return' keyword in Python. Includes analogies, code examples, and 'print vs return' debugging tasks.

Welcome back, coders! Imagine if you went to a vending machine, paid your money, typed in the code for a chocolate bar, and the machine just shouted "Chocolate!" at you but didn't actually give you anything to eat. Useless, right? Up until now, our subroutines have been doing exactly that - printing answers to the screen but not giving the main program the actual data to work with. Today, we fix that. We are going to learn about the
return command, which turns your functions into powerful data factories that calculate answers and send them back to be used. This is exactly how professional Software DevelopersI have no idea what this means build complex banking apps and games - by creating small, flexible, reusable functions that pass data around!Learning Outcomes
The Building Blocks (Factual Knowledge)
The Connections and Theories (Conceptual Knowledge)
The Skills and Methods (Procedural Outcomes)
Recall that the
return keyword is used to send a value back from a function to the line of code that called it.Identify that a function execution stops immediately when a
return statement is reached.State that if a function does not return a value, it evaluates to
None when assigned to a variable.The Connections and Theories (Conceptual Knowledge)
Describe the difference between
print (displaying to a user) and return (passing data back to the program).Explain how a function call can be treated as a value within an expression (e.g.,
if check_score() > 100:).Analyse code to trace the flow of data out of a function and into a variable.
The Skills and Methods (Procedural Outcomes)
Apply the
return statement to create functions that perform calculations and output results.Create programs that capture returned values into variables for further processing.
Debug logical errors caused by confusing "printing" with "returning".
Digital Skill Focus: Organise code files using meaningful filenames (e.g.,
calc_functions.py) to maintain a structured digital workspace.The 'Return' of the King
In our previous lessons, we wrote procedures that looked like this:
To fix this, we use the
return keyword which turns our procedure into a function. Think of a function like a chef in a kitchen.Parameters are the ingredients you give the chef.
The Function Body is the cooking process.
Return is the chef putting the finished meal on the counter for the waiter to take away.
If the chef just ate the meal (printed it to the console), the customer (the main program) would starve! Let's look at how we can adapt the first script to ensure that the result of the function is delivered to the calling statement.
As we can see, the value calculated by the functions is returned to the expression calling it and saved in the variable
answer. We can use and manipulate this variable as many times as we like.Capturing the Value
When a function returns a value, the function call itself evaluatesI have no idea what this means to that value. This means you can treat the function call exactly like a number or a string. You have two main choices when calling a returning function:
1
Store it in a variable (The most common way)
We saw this in the example above. The advantage of doing this is that the value returned from the function is reusable.
2
Use it directly in an expression
This makes your code incredibly efficient because you don't need to create temporary variables to hold the return values in order to process it. However, this method is no good if you want to reuse the return value because it's lost soon after it's used.

Task 1 The Value Catcher
It's time to build your own data factory. You are going to write a program that calculates the area of rectangles, but the calculation MUST happen in a function.
1
Get Organised!
1
Open the Thonny IDE on your computer.
2
If it's the first time you've run Thonny today, you'll get a blank script.
3
Organise your workspace.
2
Define the Function
1
Define a function called
calc_area which takes two parameters, the width and the height of the rectangle:def calc_area(width,height):
2
Inside the function, calculate the area (notice the indentationI have no idea what this means):
area = width*height
3
CRITICAL: Use the
return keyword to send the result back. Do NOT print inside the function (notice the indentation): return area
3
The Main Program
1
After the function, ask the user for a width and a height (remember to cast to
int).w = int(input("Width: "))
h = int(input("Height: "))
2
Call your function and store the result in a variable called
area.area = calc_area(w, h)
3
Finally, output/print the answer politely:
print(f"The total area is {area}")
4
Run your area calculator
Press F5 or click the green run button. You will be asked to save your Python file before you can run it. Give it the filename area_calc.py and save it in a suitable place in your documents. You should see a promptI have no idea what this means in the shell (the bottom part of the Thonny window) waiting for you to enter the width. Make sure you press ENTER afterwards. Then enter the height when prompted, press ENTER again and you should see your result.
Outcome: A working program that separates calculation (function) from input/output (main program).

Using multiple return values
We aren't restricted to just one return value in a function. We can have as many as we like as long as only one return keyword is ever executed. We would generally accomplish this using the
if...elif...else selection structure.Pass or Fail depending on the value of score. As soon as a return keyword is reached, the function "ejector seat" is fired.
Task 2 The Logic Gate
You need to write a function that calculates the grade for a student based on their percentage score.
1
Get Organised!
1
Open up the Thonny IDE.
2
Create a new script (or replace the previous one).
3
Organise your workspace.
2
The Function
1
First, we'll define a function
get_grade which takes one parameter, score:def get_grade(score):
2
Now we'll construct the "Ejector Seat" logic:
if score > 90, return "A"
else if score > 70, return "B"
else if score > 50, return "C"
else return "Fail"
if score > 90:
return "A"
elif score > 70:
return "B"
elif score > 50:
return "C"
else:
return "Fail"
The order matters here. You must check the values in descending order for this to work.
3
Get the mark
In the main program, we'll ask the user for a mark and print their grade using your function.
mark = int(input("Enter mark: "))
print(get_grade(mark))
4
Run it!
Press F5 or click the green Run button. You will be asked to save your file, call it grader.py and make sure it's saved in a suitable place in your documents. Now repeatedly run your script and test the following values:
100
91
90
71
70
51
50
49
Can you see why we are using these values to test our function? Is the output what you expected? These are called boundary testsI have no idea what this means.
Outcome: Using multiple return points to simplify logic.

Out of Lesson Learning
Last modified: January 26th, 2026
