GCSE · Computer Science · AQA · Spec 8525

Procedures and functions

Typing the same code fifty times means fifty fixes when it's wrong. So you write it once, name it, and call it. Let's follow one call there and back.

Computer Science · Subroutines

Follow the call: into bonus and back again

Press Next and keep your eye on the highlighted line. Where does the program go? Where does each value land? What comes back?

Predict before you press Next: what will score hold when the program reaches OUTPUT?

1 SUBROUTINE bonus(points, level)
2 extra ← points * level
3 RETURN extra
4 ENDSUBROUTINE
5 score ← 50
6 score ← score + bonus(10, 3)
7 OUTPUT score

Variables

score=50

Output

 

Step 1: Notice something odd? The program starts at line 5, not line 1. Lines 1 to 4 only define bonus, and defining a subroutine doesn't run it. score is set to 50.

1 / 8

Watch the Variables panel: points, level and extra pop into existence when bonus starts, and vanish when it returns.

Step 1 of 8: Notice something odd? The program starts at line 5, not line 1. Lines 1 to 4 only define bonus, and defining a subroutine doesn't run it. score is set to 50..

Exam line: Tell it in order: the call jumps to the subroutine, the arguments are passed to the parameters, RETURN sends a value back, and that value replaces the call.
Watch out: bonus is written first but runs second. Code inside a subroutine definition does nothing until the subroutine is called.

Computer Science · Subroutines

Procedure or function?

Pick a subroutine, then choose its kind. Each one is shown by its name and what its body does. One of them is trying to trick you.

Still to sort

Function (0)

Returns a value to the code that called it.

Where the line is: RETURN sends a value back to the caller. Showing a value on screen with OUTPUT is not the same thing.

Procedure (0)

Carries out its task without returning a value.

Where the line is: A procedure can still take parameters and can still display things. It just sends nothing back.

5 of 5 still to sort.

Every subroutine is one or the other. Ask just one question: does it send a value back to the caller with RETURN?

Computer Science · Debugging

Find the bug: where did the value go?

This program should work out how many seats a cinema screen has (5 rows of 8 seats, plus 2 wheelchair spaces) and output 42. It doesn't. The maths is fine. So which line is wrong?

A student's program — which line goes wrong?

Computer Science · Local and global variables

Same name, same box?

The main program has a global variable called count, holding 10. A subroutine called reset declares its own local variable, also called count, and sets it to 0. The main program calls reset(), then runs OUTPUT count.

What does the main program output? Go with your gut, then pick the idea closest to it.
How sure are you?

Computer Science · Structured programming

One quiz game, built from modules
Main programgetAnswerisCorrectshowScorein: questionback: answerin: answer, correctback: True/Falsein: scoreback: nothing

Showing 2 layers: Structure chart, Interfaces

Layers

Explore

Turn Interfaces off to see the bare decomposition, then switch it back on.

Each module's interface: the data that goes in through its parameters, and the value, if any, that comes back.

Tap a box, or its name under Explore, to read its interface: what goes in, and what comes back.

Computer Science · Subroutines

Your turn: why bother with subroutines?

A programmer is writing a large program. Explain two advantages of building it from subroutines. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • A subroutine is a self-contained block of code with its own identifier, built to do one specific task.
  • Call it by its identifier and control jumps there; when it finishes, control comes back to where the call was made.
  • There are two kinds. A function returns a value to the code that called it; a procedure returns nothing.
  • OUTPUT is not RETURN: putting a value on the screen sends nothing back to the calling code.
  • Parameters are the variables named in a subroutine's definition. Arguments are the values you put in the call.
  • Have a goSUBROUTINE move(steps, dir) is called as move(4, "left"). Sort the four things you can see into parameters and arguments.

    Parameters: steps and dir. Arguments: 4 and "left".

    Parameters are the names written in the definition; arguments are the values written in the call. Swapping the two words round is the classic slip.

  • Arguments are passed to parameters in order, first to first, so one subroutine can work on different data.
  • A returned value replaces the call, so you can store it in a variable, output it or use it in an expression.
  • Have a gosq(n) returns n * n. What does y ← sq(sq(2)) store? Work from the inside out.

    16

    sq(2) returns 4, and that 4 replaces the inner call, so the line becomes y ← sq(4), which returns 16. Answering 4 or 8 forgets that the returned value is fed into the outer call.

  • A local variable is declared inside a subroutine, exists only while it runs and can only be accessed from inside it.
  • Have a goJordan's subroutine counts 12 cookies into a local variable called cookies, then finishes. Nothing else is called cookies. The main program runs OUTPUT cookies. What does Jordan learn the hard way?

    The main program can't use cookies at all. It doesn't exist there.

    cookies was local, so it could only be accessed inside the subroutine, and it vanished when the subroutine finished. The 12 never left; only RETURN could have sent it back.

  • A global variable is declared in the main program and can be accessed from anywhere in the program.
  • Structured programs are built from modules (subroutines), each with a clear interface: parameters in, returned value (if any) out.

The big picture

A subroutine is a named, self-contained block of code that does one job. Call it, and control jumps to it; when it finishes, control comes back to where the call was made. The arguments in the call are passed to the parameters in the definition, in order. A function returns a value, and that value replaces the call in the calling code; a procedure returns nothing. Local variables live only inside their subroutine while it runs, which keeps subroutines self-contained, and structured programs are built from subroutines with clear interfaces.

Key points

1Call, jump, run, return, carry on: control always comes back to the point of the call.
2The deciding test is RETURN. A value sent back makes it a function; nothing sent back makes it a procedure. Outputting is not returning.
3Arguments are matched to parameters by position: first to first, second to second.
4Same identifier, different variables: a local variable can't change a variable of the same name anywhere else, which keeps the subroutine self-contained.
5Subroutines let you decompose a problem, reuse code without repeating it, test parts separately, make a change in one place and share the work.

Worked example

Problem

SUBROUTINE area(w, h) holds two lines: a ← w * h, then RETURN a. The main program runs x ← area(2, 5), then y ← area(x, 3) - 1, then OUTPUT y. What is output?

⚠ Watch out

Mixing up parameters and arguments. The parameters are the names in the subroutine's definition, like w and h in area(w, h). The arguments are the values in the brackets when you call it, like 2 and 5 in area(2, 5). Definition: names. Call: values.

🧠

Memory hook

A function is a round trip that brings back a souvenir: off it goes, does the job and comes home with a value. A procedure makes the same trip and comes home empty-handed.

✓

Check yourself

No peeking: cube(n) returns n * n * n. In y ← cube(2) - 3, where does control go at the call, which parameter gets 2, and what does y hold? (y holds 5.)

Flashcards

(14)
What is a subroutine?
A self-contained block of code, with its own identifier, that carries out one specific task.
What happens to control when a subroutine is called?
It jumps to the subroutine, the subroutine's code runs, and then control comes back to the point where the call was made.
What is the one difference between a function and a procedure?
A function returns a value to the code that called it. A procedure carries out its task without returning a value.
A subroutine OUTPUTs its answer but has no RETURN. Which kind is it?
A procedure. OUTPUT puts the value on the screen; only RETURN sends it back to the calling code.
What is a parameter?
A variable listed in a subroutine's definition that receives data passed into the subroutine.
What is an argument?
A value supplied in the call, which is passed to one of the subroutine's parameters.
How are arguments matched to parameters?
In order: the first argument goes to the first parameter, the second to the second, and so on.
Where does a function's returned value go?
It replaces the call in the calling code, so it can be assigned to a variable, output or used in an expression.
What is a local variable?
A variable declared inside a subroutine. It exists only while that subroutine is running and can only be accessed from within it.
What is a global variable?
A variable declared in the main program. It can be accessed from anywhere in the program, and it lasts while the program runs.
Why does using local variables make subroutines easier to reuse, test and debug?
Each subroutine stays self-contained: changes inside it can't accidentally alter data used elsewhere in the program.
A rule in a calculation changes. Why is that easier if the calculation is a subroutine?
You change it in one place, inside the subroutine, and every call to it uses the new version.
What is structured programming?
Building a program from modules (subroutines), each with a clear interface.
What is a subroutine's interface?
The data it takes in through its parameters, and the value, if any, that it returns.

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 28 October 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.