AQA GCSE CSPaper 1 · Unit 2Lesson 11

Paper 1 · Unit 2 · CS-L2-11

Subroutines — Procedures & Functions

60 minutes · AQA 8525 · Paper 1 — Programming

Spec & Goals 3 min

AQA Spec 3.2.10 · Subroutines (procedures and functions)

By the end of this lesson you can:

  1. Define a subroutine and explain how it is called by name.
  2. State the difference between a procedure and a function that returns a value.
  3. Write a subroutine with parameters in AQA pseudo-code and in Python.

Warm-Up 5 min

Last lesson you used string handling — LEN, SUBSTRING and joining text together.

Quick starter

A program calculates the area of three different rooms. The same three lines of code are copied out three times. Name one problem this causes.

Reveal an answer

Repeated code is harder to read and to maintain. If the formula is wrong, you must fix it in three places. A subroutine lets you write the steps once and reuse them.

Key Concept — naming a block of code 14 min

A subroutine is a named block of code you can call by its name whenever you need it. The program runs the block, then carries on from where it was called.

Two kinds: procedure and function

AQA splits subroutines into two kinds:

So a function gives you a value back that you can use; a procedure simply does its job. A function is called inside an expression, such as total ← area(4, 5).

Parameters

A subroutine listing such as SUBROUTINE area(width, height) declares two parameters. When you call area(4, 5), the values 4 and 5 are passed in.

Why use subroutines

  • Reuse — write the code once, call it many times.
  • Readability — a sensible name explains what a block does.
  • Decomposition — break a big problem into smaller named parts.
  • Easier testing — test each small subroutine on its own.

Worked Example — an area function and a greeting procedure 12 min

Problem: write a function that returns the area of a rectangle, and a simple procedure that prints a welcome message.

AQA pseudo-code

SUBROUTINE area(width, height)
  RETURN width * height
ENDSUBROUTINE

SUBROUTINE greet()
  OUTPUT 'Selamat datang to the hall booking system'
ENDSUBROUTINE

# Call the function inside an expression
OUTPUT area(4, 5)

# Call the procedure on its own
greet()

The call area(4, 5) passes 4 and 5 in as the parameters. The function returns 20, so OUTPUT area(4, 5) displays 20. The procedure greet() returns nothing — it just prints its message.

The same in Python

def area(width, height):
    return width * height

def greet():
    print("Selamat datang to the hall booking system")

# Call the function inside an expression
print(area(4, 5))

# Call the procedure on its own
greet()

Try It Yourself 12 min

🟢 Easy

Goal: Write a procedure banner() that outputs the line 'Pasar Malam Stall Manager'.

Hint: a procedure has no RETURN — just an OUTPUT inside SUBROUTINE … ENDSUBROUTINE.

🟡 Medium

Goal: Write a function addTax(price) that returns the price with 6% SST added.

Hint: RETURN price * 1.06, then call it with OUTPUT addTax(10).

🔴 Stretch

Goal: Write a function larger(a, b) that returns the larger of its two parameters, then call it to output the larger of 12 and 9.

📝 Exam Practice 10 min

Answer the way the examiner expects — the command word and the marks tell you how much to write.

Define[1 mark]

Define the term subroutine.

Mark scheme
  • A named block of code that can be called by name (from elsewhere in the program) (1).
State[1 mark]

State one difference between a procedure and a function.

Mark scheme
  • A function returns a value (using RETURN) but a procedure does not (1).
Give[2 marks]

Give two benefits of using subroutines in a program.

Mark scheme
  • Code can be reused / written once and called many times (1).
  • The program is easier to read / a named block shows its purpose (1).
  • Allows decomposition into smaller parts (1).
  • Each subroutine is easier to test on its own (1).
  • (Max 2 — any two valid benefits.)
State[1 mark]

Look again at the worked-example pseudo-code. State the value output by OUTPUT area(4, 5).

Mark scheme
  • 20 (1) — because the function returns width * height = 4 * 5.

Recap & Key Terms 3 min

A subroutine is a named block of code you call by name. A procedure carries out a task; a function carries out a task and returns a value. Parameters pass data in. Subroutines aid reuse, readability, decomposition and testing.

Subroutine
A named block of code that can be called by name from elsewhere in a program.
Procedure
A subroutine that carries out a task but does not return a value.
Function
A subroutine that carries out a task and returns a value using RETURN.
Parameter
A value passed into a subroutine so it can work on different data each time.
Return value
The value a function sends back to the point where it was called.

Homework 1 min

Task (≤ 15 min): In AQA pseudo-code, write a function cube(n) that returns n multiplied by itself three times, then call it to output the cube of 3.

Model answer
SUBROUTINE cube(n)
  RETURN n * n * n
ENDSUBROUTINE

OUTPUT cube(3)

Award marks for: a correct subroutine header with a parameter (1), a correct RETURN expression (1), and a correct call that outputs 27 (1).