TopMyGrade

GCSE/Computer Science/AQA

CS2.9Subroutines (procedures and functions): parameters and return values; local vs global variables; benefits of using subroutines

Notes

Subroutines — procedures and functions

A subroutine is a named block of code you can call from elsewhere in your program. AQA distinguishes two kinds:

  • Procedure — performs a task; doesn't return a value.
  • Function — performs a calculation and returns a value to the caller.

Both can take parameters — values passed in by the caller.

Why use subroutines?

  1. Decomposition — a big problem becomes several smaller ones.
  2. Reuse — write once, call from many places.
  3. ReadabilitycalculateTotal(items) reads better than 12 lines of inline code.
  4. Testability — small subroutines are easier to test in isolation.
  5. Maintenance — fix a bug in one place, not twelve.

Procedure syntax

PROCEDURE greet(name)
  OUTPUT "Hello, " + name
ENDPROCEDURE

-- call site:
greet("Aisha")

Function syntax

FUNCTION square(n)
  RETURN n * n
ENDFUNCTION

-- call site:
SET result TO square(5)   -- 25
OUTPUT result

The RETURN statement immediately exits the function and hands the value back to whoever called it.

Parameters and arguments

  • Parameter — the variable name in the subroutine definition (n above).
  • Argument — the actual value passed at the call site (5 above).

You can have several parameters, separated by commas:

FUNCTION rectangleArea(width, height)
  RETURN width * height
ENDFUNCTION

OUTPUT rectangleArea(4, 6)   -- 24

Local vs global variables

A local variable is declared inside a subroutine and only exists for the duration of that call. A global variable is declared outside any subroutine and is visible everywhere.

SET total TO 0     -- global

PROCEDURE addOne()
  SET total TO total + 1
ENDPROCEDURE

PROCEDURE counter()
  SET local TO 0   -- local
  SET local TO local + 1
  OUTPUT local
ENDPROCEDURE

Best practice: prefer local variables. Globals make programs harder to reason about because anything can change them. Pass values via parameters and return results.

Worked exampleWorked example — refactor with subroutines

Without subroutines:

USERINPUT a
USERINPUT b
SET aSquared TO a * a
SET bSquared TO b * b
SET sum TO aSquared + bSquared
SET hyp TO SQRT(sum)
OUTPUT hyp

With a function:

FUNCTION hypotenuse(a, b)
  RETURN SQRT(a * a + b * b)
ENDFUNCTION

USERINPUT a
USERINPUT b
OUTPUT hypotenuse(a, b)

The function can now be called from anywhere with any pair of values — that's reuse.

Returning multiple values

AQA pseudocode doesn't natively return multiple values. Workarounds:

  • Return an array: RETURN [min, max].
  • Return a record (if available).
  • Use multiple functions, each returning one value.

Recursion (extension)

A subroutine that calls itself. Not core to GCSE but recognise it:

FUNCTION factorial(n)
  IF n <= 1 THEN
    RETURN 1
  ELSE
    RETURN n * factorial(n - 1)
  ENDIF
ENDFUNCTION

Common mistakesPitfalls

  1. Forgetting to RETURN — function returns nothing/undefined.
  2. Forgetting parameters — calling square() instead of square(5).
  3. Modifying globals from inside a subroutine — surprises the caller.
  4. Treating procedures as functionsSET x TO greet("Aisha") won't work because greet is a procedure.
  5. Variable name clashes — local n inside the function shadows global n.

Try thisQuick check — function or procedure?

For each task, decide:

  • "Calculate the area of a circle from a radius." → Function (returns a value).
  • "Print a banner of asterisks." → Procedure (no value to return).
  • "Convert a string to uppercase." → Function (returns the new string).
  • "Save data to a log file." → Procedure (side-effect, no return).

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 12 marks

    Procedure vs function

    State two differences between a procedure and a function.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 25 marks

    Write a function

    Write a function called areaCircle that takes a radius and returns the area of a circle (use π = 3.14).

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 33 marks

    Local vs global

    Explain the difference between a local and a global variable, and state one advantage of using local variables.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 43 marks

    Parameter vs argument

    In the call max(7, 12), identify which value is an argument and explain the difference between argument and parameter.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 53 marks

    Benefits of subroutines

    State three benefits of using subroutines when designing a large program.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Refactor to a function

    Refactor this pseudocode by introducing a function called isEven that takes an integer and returns true if it's even.

    USERINPUT n
    IF n MOD 2 = 0 THEN OUTPUT "even" ELSE OUTPUT "odd" ENDIF
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Trace a function call

    Trace the output of:

    FUNCTION op(a, b)
      RETURN a * b - a
    ENDFUNCTION
    OUTPUT op(3, 5)
    OUTPUT op(10, 2)
    
    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.9 — Subroutines (procedures and functions)

11-card SR deck for AQA GCSE Computer Science topic CS2.9

11 cards · spaced repetition (SM-2)