SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 1 · 4.1.2c

Subroutines, Parameters
& Recursion

AQA A-Level Computer Science · Section 4.1 Fundamentals of Programming

WHAT YOU'LL LEARN
SUBROUTINE · FUNCTION · Parameters vs arguments · By value vs reference · Recursion
AQA SPEC LINK
4.1.2 — Subroutines, local/global scope, parameter passing, recursion
Subroutines

What is a Subroutine?

A subroutine is a named, reusable block of code that performs a specific task. Calling a subroutine transfers control to it; when it finishes, control returns to the calling code.
PROCEDURE (AQA)
SUBROUTINE greet(name)
  OUTPUT "Hello " + name
ENDSUBROUTINE

greet("Alice")
FUNCTION (returns value)
SUBROUTINE square(n)
  RETURN n * n
ENDSUBROUTINE

result ← square(5) → 25
Parameters

Parameters vs Arguments

SUBROUTINE add(a, b)    ← a, b are PARAMETERS
  RETURN a + b
ENDSUBROUTINE

result ← add(3, 7)       ← 3, 7 are ARGUMENTS
PARAMETERS
Variable names in the definition of the subroutine
ARGUMENTS
Actual values passed when calling the subroutine
Passing Parameters

By Value vs By Reference

PASS BY VALUE
A copy of the value is passed. Changes inside the subroutine do not affect the original variable.
SUBROUTINE double(x)
  x ← x * 2   ← local copy only
ENDSUBROUTINE
PASS BY REFERENCE
The memory address is passed. Changes inside the subroutine do affect the original variable.
Used for modifying arrays or large structures without copying.
Scope

Local vs Global Variables

LOCAL VARIABLE
Declared inside a subroutine. Only accessible within that subroutine. Destroyed when subroutine exits.
GLOBAL VARIABLE
Declared outside all subroutines. Accessible anywhere in the program. Persists for program lifetime.
⚠️ AQA best practice: prefer local variables and pass values as parameters. Overuse of globals makes programs harder to debug.
Recursion

What is Recursion?

Recursion is when a subroutine calls itself. Every recursive solution needs:
1. A base case — stops the recursion
2. A recursive case — moves towards the base case
FACTORIAL EXAMPLE
SUBROUTINE factorial(n)
  IF n = 1 THEN                   ← BASE CASE
    RETURN 1
  ELSE
    RETURN n * factorial(n-1)    ← RECURSIVE CASE
  ENDIF
ENDSUBROUTINE
Call Stack

How Recursion Uses the Call Stack

factorial(4) unwinds like this:
factorial(4) → 4 × factorial(3)
factorial(3) → 3 × factorial(2)
factorial(2) → 2 × factorial(1)
factorial(1) → 1 [BASE CASE — RETURNS]
← 2×1=2 ← 3×2=6 ← 4×6=24
Each recursive call adds a frame to the call stack. Too many calls without hitting the base case causes a stack overflow.
Benefits & Risks

Recursion: Pros & Cons

✅ ADVANTAGES
Elegant, concise solutions for recursive problems
Natural fit for tree traversal, fractals, sorting
Often matches mathematical definitions directly
⚠️ DISADVANTAGES
Stack overflow if base case missing or unreachable
Uses more memory than iterative solutions
Can be harder to trace and debug
AQA Exam Style

Practice Question

AQA 7517 — Paper 1 Style
The following recursive function calculates the sum of integers from 1 to n:

SUBROUTINE sumTo(n)
  IF n = 1 THEN RETURN 1
  ELSE RETURN n + sumTo(n-1)
  ENDIF
ENDSUBROUTINE

What is the value returned by sumTo(5)? Show your working.
[3 marks]
1 mark
sumTo(5) = 5 + sumTo(4) = 5 + 4 + sumTo(3) = ... = 5+4+3+2+1 = 15
1 mark
Base case: sumTo(1) returns 1 — recursion stops here
1 mark
Each call reduces n by 1, moving towards base case
Summary

Key Points to Remember

Subroutine — named block of reusable code; procedure (no return) or function (returns value)
Parameters — named in definition; Arguments — values passed on call
By value — copy passed, original unchanged; By reference — address passed, original can change
Local — inside subroutine only; Global — accessible everywhere
Recursion — calls itself; needs a base case + recursive case; uses call stack
🎉 Section 4.1 complete — on to Data Structures!