TopMyGrade

GCSE/Computer Science/OCR

2.2.8Subroutines (procedures and functions): parameters and return values; local vs global variables; benefits

Notes

Subroutines: procedures and functions

A subroutine is a named block of code that performs a specific task and can be called (invoked) from multiple places in a program. OCR J277 Paper 2 tests writing, calling and tracing subroutines, distinguishing procedures from functions, and explaining the benefits of subroutines.

Types of subroutine

Procedure

  • Performs a task but does not return a value.
  • Called by using its name; execution returns to the calling code when the procedure ends.
PROCEDURE greet(name)
    OUTPUT "Hello, " + name + "!"
END PROCEDURE

// Calling the procedure
greet("Alice")    // outputs: Hello, Alice!
greet("Bob")      // outputs: Hello, Bob!

Function

  • Performs a task and returns a value to the calling code.
  • Called within an expression or assignment.
FUNCTION square(n)
    RETURN n * n
END FUNCTION

result = square(5)    // result = 25
OUTPUT square(3)      // outputs: 9

Parameters and arguments

  • A parameter is a variable in the subroutine definition that receives a value when the subroutine is called.
  • An argument is the actual value passed to the subroutine when it is called.
FUNCTION add(a, b)    // a and b are parameters
    RETURN a + b
END FUNCTION

total = add(10, 20)   // 10 and 20 are arguments

Multiple parameters are separated by commas. Parameters make subroutines reusable — the same code can operate on different data each time it is called.

Local vs global variables

Local variables

  • Declared inside a subroutine.
  • Only accessible within that subroutine.
  • Created when the subroutine is called; destroyed when it finishes.

Global variables

  • Declared outside all subroutines (at the program level).
  • Accessible from anywhere in the program.
  • Persist for the entire program's lifetime.

Why prefer local variables?

  • Encapsulation: subroutines work independently without depending on external state.
  • Avoids naming conflicts: two subroutines can each have a local variable called i without conflict.
  • Easier to debug: if a variable has a wrong value, only the code inside that subroutine could have caused it.
x = 10    // global variable

FUNCTION calculate(n)
    result = n * 2    // local variable
    RETURN result
END FUNCTION

OUTPUT calculate(x)   // outputs: 20
// 'result' does not exist here — it was local to calculate()

Benefits of using subroutines

  1. Reusability: write once, call many times — avoids code duplication.
  2. Decomposition: breaks a large program into manageable, single-purpose blocks.
  3. Easier testing: each subroutine can be tested independently.
  4. Easier maintenance: changing one subroutine updates all places that call it.
  5. Readability: well-named subroutines make code self-documenting.

Common OCR exam mistakes

  1. Confusing procedure and function — a procedure does NOT return a value; a function DOES.
  2. Trying to use a local variable outside its subroutine — local variables do not exist beyond the subroutine.
  3. Forgetting that parameters are local to the subroutine — they cannot be accessed by the calling code.
  4. Calling a function but not capturing or outputting the return value — the result is lost.

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

Practice questions

Try each before peeking at the worked solution.

  1. Question 15 marks

    Procedure vs function

    (a) State the difference between a procedure and a function. [2]
    (b) Write an OCR pseudocode function called cube that takes one parameter n and returns n cubed. [3]

    Ask AI about this

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

  2. Question 23 marks

    Local vs global variables

    A student writes the following program:

    score = 0    // line 1
    
    PROCEDURE addPoints(points)
        score = score + points    // line 3
    END PROCEDURE
    
    addPoints(10)
    OUTPUT score
    

    (a) Is the variable score on line 1 local or global? [1]
    (b) The student says that the score variable inside the procedure is a different variable from the one on line 1. Is this correct? Explain. [2]

    Ask AI about this

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

  3. Question 34 marks

    Benefits of subroutines

    Give two benefits of using subroutines when writing a large program. [4 marks]

    Ask AI about this

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

Flashcards

2.2.8 — Subroutines (procedures and functions): parameters and return values; local vs global variables; benefits

8-card SR deck for OCR Computer Science (J277) topic 2.2.8

8 cards · spaced repetition (SM-2)