GCSE · Computer Science · AQA · Spec 8525

Concept of subroutines

Some of the most useful code in a program doesn't run when the program starts. It just waits — until something calls it.

Computer Science · Subroutines

Follow the highlight

A tiny game program. The subroutine levelUp is written once, at the top, and called twice further down. Press Next and watch where the highlighted line goes.

Keep this question in your head as you step: when levelUp reaches ENDSUBROUTINE on line 4, which line runs next?

1 SUBROUTINE levelUp()
2 OUTPUT "* * * * *"
3 OUTPUT "Level complete!"
4 ENDSUBROUTINE
5 # main program
6 level ← 1
7 levelUp()
8 level ← 2
9 levelUp()
10 OUTPUT "You win!"

Variables

level=1

Output

 

Step 1: The program starts here, on line 6. Lines 1–4 were skipped: they only define levelUp, and defining a subroutine doesn't run it.

1 / 11

Use Next and Back. Watch the highlight leave the main program — and come back.

Step 1 of 11: The program starts here, on line 6. Lines 1–4 were skipped: they only define levelUp, and defining a subroutine doesn't run it..

Watch out: When a subroutine finishes, the program doesn't drop down to the next line of the file, and it doesn't restart. It goes back to the line just after the call.

Name the parts

The parts of a subroutine
1SUBROUTINElevelUp()2OUTPUT "* * * * *"3OUTPUT "Level complete!"4ENDSUBROUTINE5# main program6level ← 17levelUp()8level ← 29levelUp()10OUTPUT "You win!"

Showing 1 layer: Program

Explore

Tap a part of the program.

One subroutine definition at the top, then the main program underneath.

It's the same program you just stepped through. Tap each part to see what it's called and what it does.

What do you think?

Be honest: which one is closest to your idea?

Ana's program defines a subroutine called greet() at the top. greet() outputs "Hello". Underneath, her main program has three lines: OUTPUT "Start", then greet(), then OUTPUT "End".

What happens when Ana runs her program?
How sure are you?

Your turn to predict

One subroutine calling another

The main program is: OUTPUT "Morning!", then getReady(), then OUTPUT "Out the door". Inside getReady() are: OUTPUT "Get dressed", then packBag(), then OUTPUT "Shoes on". Inside packBag() is one line: OUTPUT "Pack your bag". The seven statements below are listed in the order they're written. Put them in the order they actually run.

1 · Runs first7 · Runs last
  1. OUTPUT "Morning!"

    main program, 1st line

  2. getReady()

    main program, 2nd line

  3. OUTPUT "Out the door"

    main program, 3rd line

  4. OUTPUT "Get dressed"

    inside getReady(), 1st line

  5. packBag()

    inside getReady(), 2nd line

  6. OUTPUT "Shoes on"

    inside getReady(), 3rd line

  7. OUTPUT "Pack your bag"

    inside packBag(), its only line

Two kinds of subroutine

Procedure or function?

Sort each subroutine, or each call, into the right column.

Still to sort

Procedure (0)

Carries out a task. Sends no value back.

Where the line is: Putting something on the screen doesn't count as sending a value back. Nothing reaches the code that made the call.

Function (0)

Carries out a task and returns a value to the caller.

Where the line is: Because a value comes back, a function call can be used where a value is needed — inside an assignment or an expression.

6 of 6 still to sort.

One test decides every one: does the subroutine send a value back to the code that called it?

Watch out: OUTPUT and RETURN are not the same thing. OUTPUT shows something to the person using the program. RETURN hands a value back to the code that made the call.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Writing a subroutine doesn't run it. Calling it does — and when it's finished, the program picks up exactly where it left off.

What you need to know

  • What a subroutine is, and what its identifier is for
  • Why defining a subroutine doesn't make it run
  • Where control goes when a subroutine is called, and where it comes back to when it finishes
  • How to trace a subroutine that is called more than once, or that calls another subroutine
  • The difference between a procedure and a function

The big picture

Subroutines are named, self-contained blocks of code, each doing one job, written apart from the main program. Defining one doesn't run it; a call does. The call sends control to the subroutine's first statement, and when it finishes, control returns to just after the call. A subroutine can be called many times, from different places, and can call another subroutine. A procedure sends no value back; a function returns a value to its caller, so its call can sit inside an assignment or expression.

Key points

1A subroutine is a self-contained block of code that carries out one specific task. It has its own identifier (its name) and is written separately from the main program, not in line with it.
2Defining a subroutine does not run it. Its statements run only when another part of the program calls it using its identifier, e.g. levelUp().
3When a subroutine is called, control passes from the call to the subroutine's first statement. When the subroutine finishes, control returns to the point just after the call, and the program carries on from there.
4The same subroutine can be called many times, from different places. Each call returns to just after itself.
5One subroutine can call another. The inner subroutine returns to the outer one first; the outer one returns to wherever it was called from when it finishes.
6A procedure carries out a task but sends no value back. A function carries out a task and returns a value to the code that called it, so a function call can be used inside an assignment or expression, e.g. lives ← getMaxLives().

Worked example

Problem

Here is a program: 1 SUBROUTINE divider() 2 OUTPUT "-----" 3 ENDSUBROUTINE 4 SUBROUTINE banner() 5 divider() 6 OUTPUT "QUIZ TIME" 7 divider() 8 ENDSUBROUTINE 9 # main program 10 banner() 11 OUTPUT "Question 1" 12 divider() Write down everything the program outputs, in order.

⚠ Watch out

Tracing a program straight down the page, so the subroutine runs where it's written and the program carries on after ENDSUBROUTINE. Follow the calls: a subroutine runs when called, then control returns to just after the call.

🧠

Memory hook

Call, jump in, run, jump back. Every return lands on the line just after its own call — and a function brings a value back with it.

✓

Check yourself

beep() outputs "Beep". A main program runs beep(), OUTPUT "Hi", then beep(). Write the full output. Is beep() a procedure or a function — and how can you tell?

Flashcards

(11)
What is a subroutine?
A self-contained, named block of code that carries out one specific task. It's written separately from the main program.
What is a subroutine's identifier used for?
It's the subroutine's name. A call uses the identifier to say which subroutine to run.
Does defining a subroutine run it?
No. Its statements only run when the subroutine is called.
When a subroutine is called, where does control go?
From the calling statement to the first statement of the subroutine.
When a subroutine finishes, where does the program carry on from?
The point just after the call that ran it — not the line under ENDSUBROUTINE, and not the top of the program.
Can the same subroutine be called more than once?
Yes — as many times as needed, from different places. Each call returns to just after itself.
Subroutine A calls subroutine B. When B finishes, where does control go?
Back into A, to the line just after the call to B. A carries on, and returns to its own caller when it finishes.
What is a procedure?
A subroutine that carries out a task but doesn't send a value back to the code that called it.
What is a function?
A subroutine that carries out a task and returns a value to the code that called it.
Why can only a function call appear on the right of an assignment, like x ← f()?
x needs a value to store, and only a function sends one back.
A subroutine OUTPUTs a number. Does that make it a function?
No. OUTPUT shows the number to the user. A function RETURNs a value to the calling code.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)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 29 September 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.