GCSE · Computer Science · Eduqas · Spec C500QS
Subroutines with well-defined interfaces
A subroutine prints 13 on the screen. The caller's variable, which was meant to hold the answer, holds None. Where did the 13 go?
Computer Science · Subroutines
Predict the hand-over
Make each call before the table shows it. Nothing is run: every value is the lesson's, and the only thing being tested is your prediction.
def remaining(total, used):return total - usedbudget = 20spent = 7left = remaining(budget, spent)print(left)swapped = remaining(spent, budget)print(swapped)
| budget | spent | total (param) | used (param) | return value | left | swapped |
|---|---|---|---|---|---|---|
| 20 | 7 |
Step 1. Two caller variables so far. The two def lines only describe the subroutine: nothing inside it has run yet. This first row is worked for you.
Computer Science · Subroutines
Run it and read what Python says
This program should work out 20 - 7 in a subroutine and print 13 once. Press Run, read what Python reports, then choose how to fix the flagged line.
- def remaining(total, used):
- difference = total - used
- print(difference)
- left = remaining(20, 7)
- print(difference)
The messages and outputs are what Python 3.13 gives for this exact code.
Why the inside can change
Reason it through
Why can the code inside a subroutine be rewritten without breaking the rest of the program?
First link · your turn
What does a programmer need to know in order to call a subroutine?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What goes into a subroutine, what comes back, and why nothing else should cross.
What you need to know
- A subroutine is a named, self-contained block of code that does one specific task. Other parts of the program can call it.
- A well-designed subroutine has one clear purpose, and its name describes that purpose.
- Its interface is how it connects to the rest of the program: its name, its parameters and the value it returns.
- In a call, the arguments are copied into the parameters in order: first to first, second to second.
Have a goMaya says arguments find their parameters by matching names. For def join_names(first, last), the call join_names(surname, forename) puts surname into which parameter?
first. surname is the first argument, so it goes into the first parameter.
Arguments are copied into parameters in order, so the first argument lands in the first parameter whatever the caller's variable is called.
- A result comes back to the caller through a return value, which can be stored in a variable or used in an expression.
- In Python 3, def defines a subroutine, return passes a result back, and a call looks like area = calculate_area(5, 3).
- In Python 3, a function that ends without a return statement returns None. Printing a result does not pass it back.
Have a godef double_show(n): print(n * 2) is called as x = double_show(4). The screen shows 8. What does x hold afterwards?
None
double_show has no return statement, so it gives back None. print shows 8 on the screen but does not pass it back to the caller.
- Variables created inside a subroutine are local: they exist only while it runs and cannot be accessed from outside it.
Have a goA subroutine creates a variable called bonus. Straight after the call, code outside it runs print(bonus). What happens?
It fails: bonus is local, so code outside the subroutine cannot access it.
A local variable exists only while its subroutine runs, and code outside cannot access it, so the name does not exist out there.
- A well-defined interface takes all input through parameters and gives all output through return values, not through global variables.
- So you can use it without knowing its insides, test it alone, reuse it, and improve its insides while the interface stays the same.
The big picture
A subroutine's interface, its name, its parameters and its return value, is all the rest of the program needs to know about it. Arguments go in, one value comes back out, and nothing else should cross.
Key points
Worked example
Problem
A program needs the perimeter of a rectangle that is 6 long and 4 wide, and wants to use the answer in later code. Write the subroutine and the call, and say what the caller's variable holds.
⚠ Watch out
Thinking print and return do the same job. print shows a value on the screen, but only return passes it back, so the caller's variable ends up holding None.
Memory hook
Arguments in, one value out, nothing else crosses the line. Print is for people; return is for the program.
Check yourself
Cover the page. Name the three parts of an interface, then say which part is missing when a subroutine only prints its answer.
Flashcards
(14)What is a subroutine?
What makes a subroutine well designed in terms of its job and its name?
What is the interface of a subroutine?
What is a parameter?
What is an argument?
What is a return value?
In Python 3, how do you define a subroutine and pass a result back?
In Python 3, what does a function with no return statement give back?
What is the difference between printing a result and returning it?
What is a local variable?
What does a subroutine with a well-defined interface avoid?
What does a programmer need to know to use a subroutine with a clear interface?
Why is a well-defined interface good for testing?
Name two benefits of subroutines with well-defined interfaces.
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Eduqas GCSE Computer Science topics
- Presenting test outcomes with commentary
- Program a solution to requirements
- Refining code during development
- Selection in programs
- Sequence in programs
- String manipulation in code
- Typical, extreme and erroneous test data
- Understanding revised requirements
- Using a variety of data types
- Using testing outcomes to inform development
- Validation and verification routines in code
- Variables, operators, inputs, outputs and assignment
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 16 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.