AQA GCSE CSPaper 1 · Unit 2Lesson 12

Paper 1 · Unit 2 · CS-L2-12

Variable Scope & Structured Programming

60 minutes · AQA 8525 · Paper 1 — Programming

Spec & Goals 3 min

AQA Spec 3.2.10 · Subroutines — local and global variables, structured programming

By the end of this lesson you can:

  1. Define the scope of a variable, and distinguish local from global.
  2. Explain why local variables are usually preferred over global ones.
  3. Describe structured programming and state two of its benefits.

Warm-Up 5 min

Last lesson you wrote subroutines with parameters, and saw functions return a value.

Quick starter

A variable total is created inside a subroutine. The main program then tries to print total. What do you think happens?

Reveal an answer

It causes an error — total only exists inside that subroutine. Where a variable can be used is called its scope, and that is today's topic.

Key Concept — where a variable lives 14 min

The scope of a variable is the part of the program where that variable can be used.

Local and global variables

A local variable is created when its subroutine runs and is gone when the subroutine ends. A global variable lasts for the whole program and any subroutine can change it.

Structured programming

Instead of one long block of code, you break the problem into named subroutines. Benefits: the program is easier to write, read, test, maintain and reuse.

Worked Example — local scope and a structured program 12 min

Part A: show that a local variable is not visible outside its subroutine.

AQA pseudo-code

SUBROUTINE makeTotal()
  total ← 100        # local — exists only here
  OUTPUT total
ENDSUBROUTINE

makeTotal()
OUTPUT total         # error: total is not in scope here

Inside makeTotal the variable total works fine and outputs 100. The last line fails: total is local, so it does not exist in the main program.

Part B: a short hall-booking program split into named subroutines.

SUBROUTINE getHours()
  RETURN USERINPUT
ENDSUBROUTINE

SUBROUTINE cost(hours)
  RETURN hours * 50    # RM50 per hour
ENDSUBROUTINE

h ← getHours()
OUTPUT cost(h)

The same in Python

def get_hours():
    return int(input("Hours: "))

def cost(hours):
    return hours * 50    # RM50 per hour

h = get_hours()
print(cost(h))

Try It Yourself 12 min

🟢 Easy

Goal: State whether a variable declared inside a subroutine is local or global.

Hint: think about where it can be used after the subroutine ends.

🟡 Medium

Goal: Write a function discount(price) using a local variable cut set to price * 0.1, then return price − cut.

Hint: cut stays inside the subroutine — that is what makes it local.

🔴 Stretch

Goal: Redesign a single long program that books a badminton court into three named subroutines, and say which benefit of structured programming each one shows.

📝 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 a local variable.

Mark scheme
  • A variable that can only be used inside the subroutine (block) that declares it (1).
Explain[2 marks]

Explain one advantage of using local rather than global variables.

Mark scheme
  • A local variable cannot be changed by other parts of the program (1)…
  • …so there are fewer bugs / the subroutine can be reused safely / names will not clash (1).
  • (One advantage made + developed for the second mark.)
Give[2 marks]

Give two benefits of structured programming.

Mark scheme
  • Easier to write (1).
  • Easier to read / understand (1).
  • Easier to test / debug (1).
  • Easier to maintain (1).
  • Code can be reused (1).
  • (Max 2 — any two valid benefits.)
State[1 mark]

Look again at Part A of the worked example. State what happens when the final line OUTPUT total runs.

Mark scheme
  • An error occurs because total is local / out of scope / does not exist in the main program (1).

Recap & Key Terms 3 min

Scope is where a variable can be used. A local variable lives only inside its subroutine; a global one is usable anywhere. Prefer local for fewer bugs. Structured programming splits a program into named subroutines that are easier to write, read, test, maintain and reuse.

Scope
The part of a program in which a variable can be used.
Local variable
A variable that exists only inside the subroutine that declares it.
Global variable
A variable that can be used anywhere in the program.
Structured programming
Designing a program as a set of smaller subroutines or modules, each doing one task.

Homework 1 min

Task (≤ 15 min): In AQA pseudo-code, write a function vat(price) that uses a local variable tax set to price * 0.06, then returns price + tax. Call it to output the total for 20.

Model answer
SUBROUTINE vat(price)
  tax ← price * 0.06    # local variable
  RETURN price + tax
ENDSUBROUTINE

OUTPUT vat(20)

Award marks for: a local variable used inside the subroutine (1), a correct RETURN expression (1), and a correct call that outputs 21.2 (1).