GCSE Revision Aid: This resource is designed to support your revision and may contain errors. If you find a discrepancy with your class teaching, your teacher is correct — please let us know at gcserevise@scott.scottrix.co.uk.

CS12: Subroutines

Foundation Higher AQAEdexcelOCREduqasCCEA

Understanding procedures, functions, parameters, return values, local vs global variables, and the benefits of structured programming.

Fastmail

📋 What are Subroutines?

Definition: A subroutine is a named, self-contained block of code that performs a specific task. It is defined once and can be called (invoked) from anywhere in the program. Subroutines make code more organised, reusable, and easier to debug.

There are two types of subroutines:

🔲 Procedures

A procedure is a subroutine that performs a task but does not return a value to the calling code. It may produce output, change variables, or modify data structures, but it doesn't send a result back.
Defining and Calling a Procedure
PROCEDURE greet(name)
    OUTPUT "Hello, " & name & "!"
ENDPROCEDURE

// Calling the procedure
greet("Alice")    // Output: Hello, Alice!
greet("Bob")      // Output: Hello, Bob!
Procedure with Multiple Parameters
PROCEDURE displayScore(name, score)
    OUTPUT name & " scored " & STR(score) & " points"
ENDPROCEDURE

displayScore("Alice", 95)    // Output: Alice scored 95 points
displayScore("Bob", 72)      // Output: Bob scored 72 points

🔧 Functions

A function is a subroutine that performs a calculation and returns a value to the calling code. The returned value can be stored in a variable, used in an expression, or output directly.
Defining and Calling a Function
FUNCTION calculateArea(length, width)
    area ← length * width
    RETURN area
ENDFUNCTION

// Calling the function and storing the result
roomArea ← calculateArea(5, 4)
OUTPUT "Area = " & STR(roomArea)    // Area = 20
Function in an Expression
FUNCTION circleArea(radius)
    RETURN 3.14159 * radius * radius
ENDFUNCTION

// Using the function directly in output
OUTPUT circleArea(10)    // 314.159
Function with Conditional Return
FUNCTION getGrade(score)
    IF score >= 90 THEN
        RETURN "A*"
    ELSEIF score >= 80 THEN
        RETURN "A"
    ELSEIF score >= 70 THEN
        RETURN "B"
    ELSE
        RETURN "C or below"
    ENDIF
ENDFUNCTION

OUTPUT getGrade(85)    // A
Feature Procedure Function
Returns a value? No Yes - must include RETURN
How to call As a standalone statement As part of an expression or assignment
Typical use Displaying output, modifying data Calculations, data lookups, conversions
Example displayMenu() calculateAverage(80, 90, 70)

📦 Parameters and Arguments

Parameters are the variables listed in the subroutine definition. Arguments are the actual values passed to the subroutine when it is called. Parameters act as placeholders; arguments fill them with real data.
Parameters vs Arguments
// 'name' and 'age' are PARAMETERS (placeholders)
PROCEDURE displayInfo(name, age)
    OUTPUT name & " is " & STR(age) & " years old"
ENDPROCEDURE

// "Alice" and 16 are ARGUMENTS (actual values)
displayInfo("Alice", 16)
Key Points:
Parameters: defined in the subroutine header (variables)
Arguments: the actual values passed when calling (data)
The number and order of arguments must match the parameters
Each parameter acts as a local variable inside the subroutine

🔙 Return Values

The RETURN statement sends a value back from a function to the calling code. A function can only return one value, though that value could be an array or record. Once RETURN is executed, the function ends immediately.
Return Value Examples
FUNCTION add(a, b)
    RETURN a + b              // Returns the sum
ENDFUNCTION

FUNCTION isEven(number)
    IF number MOD 2 = 0 THEN
        RETURN TRUE           // Returns Boolean TRUE
    ELSE
        RETURN FALSE          // Returns Boolean FALSE
    ENDIF
ENDFUNCTION

FUNCTION maxOf(a, b)
    IF a > b THEN
        RETURN a
    ELSE
        RETURN b
    ENDIF
ENDFUNCTION

result ← add(3, 7)         // result = 10
check ← isEven(4)          // check = TRUE
bigger ← maxOf(15, 8)      // bigger = 15

🌍 Local vs Global Variables

Global variables are accessible from anywhere in the program. Local variables exist only inside the subroutine where they are declared and cannot be accessed outside it.
Local vs Global Scope
counter ← 0                    // Global variable

PROCEDURE increment()
    localCount ← 0            // Local variable
    counter ← counter + 1    // Modifies global
    localCount ← localCount + 1
    OUTPUT "Local: ", localCount
ENDPROCEDURE

increment()    // Output: Local: 1
increment()    // Output: Local: 1 (localCount resets each call)
OUTPUT "Global: ", counter    // Output: Global: 2
Feature Local Variable Global Variable
Where declared Inside a subroutine At the top level of the program
Accessible from Only within that subroutine Anywhere in the program
Lifetime Created and destroyed with each call Exists for the entire program run
Risk of bugs Low - isolated scope High - can be changed accidentally
Best practice Preferred - use local where possible Minimise use to avoid side effects
Best practice: Use local variables wherever possible. Global variables can cause unexpected bugs because any part of the program can modify them. Local variables keep data safe and contained within their subroutine.

🏗️ Benefits of Structured Programming

Structured programming uses subroutines to break a program into manageable, self-contained modules. This approach has several major benefits.
Benefit Explanation
Reusability Write once, call many times. A calculateTax() function can be used in multiple places without rewriting the code.
Readability Subroutine names describe what the code does. displayInvoice() is clearer than 50 lines of code.
Debugging Test each subroutine independently. If a bug occurs, you know which subroutine to check.
Teamwork Different programmers can work on different subroutines simultaneously.
Maintainability Fix a bug in one place and it is fixed everywhere. Update one subroutine instead of finding every copy.
Abstraction Call a subroutine without needing to know how it works internally. Just know what it does and what it returns.
Key Principle:
Decompose your program into subroutines where each one has a single, clear purpose. This is the foundation of structured programming and modularity.

❓ Practice Questions

Q1: What is the difference between a procedure and a function?

Q2: Write a function called calculatePerimeter that takes the length and width of a rectangle and returns the perimeter.

Q3: Explain the difference between a parameter and an argument.

Q4: What is the advantage of using local variables instead of global variables?

Q5: Write a procedure called displayGrade that takes a score and outputs "Pass" if the score is 50 or above, or "Fail" otherwise.

✅ Answers

  1. A procedure performs a task but does not return a value. A function performs a calculation and returns a value using the RETURN statement. Functions are used in expressions; procedures are called as standalone statements.
  2. FUNCTION calculatePerimeter(length, width)
        perimeter ← 2 * (length + width)
        RETURN perimeter
    ENDFUNCTION
  3. Parameters are the variable names listed in the subroutine definition (placeholders). Arguments are the actual values passed to the subroutine when it is called. For example, in FUNCTION add(a, b), a and b are parameters; when calling add(3, 5), the values 3 and 5 are arguments.
  4. Local variables can only be accessed within the subroutine where they are declared, which prevents accidental modification from other parts of the program. This reduces bugs and makes the code more predictable and maintainable.
  5. PROCEDURE displayGrade(score)
        IF score >= 50 THEN
            OUTPUT "Pass"
        ELSE
            OUTPUT "Fail"
        ENDIF
    ENDPROCEDURE

🎯 Exam Tips

⚠️ Common Errors

✗ Confusing parameters and arguments ✓ Parameters are the variables defined in the subroutine header; arguments are the actual values passed when the subroutine is called.

✗ Thinking procedures and functions are the same ✓ A function RETURNS a value; a procedure does NOT return a value. Use a function when you need a result back; use a procedure for actions like displaying output.

✗ Not understanding local vs global variable scope ✓ A local variable exists only inside the subroutine where it is declared. A global variable can be accessed anywhere. Local variables prevent accidental modification of data.

✗ Forgetting to use RETURN in a function ✓ A function must include a RETURN statement to send a value back to the calling code. Without RETURN, the function behaves like a procedure and the calling code receives no result.

✍️ Model Answer

Full-Mark Response

Write a function called CalculateGrade that takes a test score as a parameter and returns a grade: 'A' for 80+, 'B' for 60-79, 'C' for 40-59, 'U' for below 40. Then write a procedure called DisplayResult that takes a name and grade and outputs a message. Show how both would be called. [6 marks]

FUNCTION CalculateGrade(score : INTEGER) RETURNS STRING IF score >= 80 THEN RETURN 'A' ELSE IF score >= 60 THEN RETURN 'B' ELSE IF score >= 40 THEN RETURN 'C' ELSE RETURN 'U' ENDIF ENDFUNCTION PROCEDURE DisplayResult(name : STRING, grade : STRING) OUTPUT name & ' achieved grade ' & grade ENDPROCEDURE grade ← CalculateGrade(75) DisplayResult('Alice', grade)

📊 AO Deep Dive

Assessment Objective Analysis

AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including subroutines: functions and procedures for AQA 8525, OCR J277 & Edexcel 1CP2.

AO2 (Application — 40%): Apply knowledge and understanding of computer science, including subroutines: functions and procedures to analyse problems in computational terms and to design, write and evaluate solutions.

AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including subroutines: functions and procedures, and make reasoned judgements about trade-offs.

📝 Exam Technique

GCSE Computer Science Exam Tips:
FUNCTION returns a value — must include RETURN. PROCEDURE does not return a value. Parameters are in the header; arguments are in the call. Use local variables inside subroutines to avoid scope issues. Always declare the return type for functions. Show the function/procedure definition AND how it is called. Advantages of subroutines: reusability, readability, easier debugging, team working.

📝 Exam Questions by Topic

🎬 Video Resources

Share this page

Ready to ace your GCSE Computer Science exams?

Get the best revision books and guides to boost your grades.