Guide 5 of 6

Functions & Procedures

Writing code in one massive block is bad practice. Learn how to create modular, reusable subroutines and how to pass data between them safely.

Exam Strategy: Top-Down Design

Cambridge examiners award high marks for Top-Down Design (also known as step-wise refinement). This is the process of breaking a large, complex algorithm down into smaller, manageable subroutines. By using Functions and Procedures, you prove to the examiner that you understand modular programming, making your code easier to read, test, and debug.

Python vs. Pseudocode Subroutines

If you code in Python, you use the def keyword to create every single subroutine. However, Cambridge pseudocode strictly forces you to categorize your subroutines into two different types depending on whether they return data or not.

Concept 🐍 Python (Wrong for Exams) 📘 Cambridge Pseudocode (Correct)
Procedure (No Return) def show_menu():
  print("Menu")
PROCEDURE ShowMenu()
  OUTPUT "Menu"
ENDPROCEDURE
Function (Returns Value) def get_max(a, b):
  return a
FUNCTION GetMax(a: INTEGER) RETURNS INTEGER
  RETURN a
ENDFUNCTION
Calling a Procedure show_menu() CALL ShowMenu()

1. Procedures vs. Functions

In standard pseudocode, subroutines are split into two specific categories:

Procedures: Perform an action (like printing a menu or updating a global variable) but do not return a value back to the main program.
Functions: Perform a calculation and always return a single value back to the main program.

2. Creating and Calling a Procedure

Procedures are defined using the PROCEDURE keyword and executed using the CALL keyword.

Procedure Syntax
PROCEDURE ShowMenu()
    OUTPUT "1. Start"
    OUTPUT "2. Quit"
ENDPROCEDURE

// The Main Program executing the procedure:
CALL ShowMenu()

3. Creating a Function

Functions require you to specify the data type they will send back using RETURNS. Inside the function, you use the RETURN keyword to pass the data back. You don't use CALL; instead, you assign the function's result directly to a variable.

Function Syntax
FUNCTION AddNumbers(Num1 : INTEGER, Num2 : INTEGER) RETURNS INTEGER
    DECLARE Total : INTEGER
    Total ← Num1 + Num2
    RETURN Total
ENDFUNCTION

// The Main Program using the function:
DECLARE Answer : INTEGER
Answer ← AddNumbers(10, 5)
OUTPUT Answer // Outputs 15

4. Parameters: BYVAL vs BYREF

When passing data (parameters) into a subroutine, you have to decide if the subroutine is allowed to permanently change the original variable.

BYVAL (By Value): Sends a copy of the data. The original variable is completely safe from being altered. By default, parameters are passed by value.
BYREF (By Reference): Sends a pointer to the original memory address. If the subroutine changes it, the original variable is permanently changed!

A-Level Exam Tip 🎓

If an exam question asks you to update an array or a record using a procedure, you must pass it BYREF, otherwise the main program won't see any of your updates!

Common Syllabus Mistakes

Examiners routinely deduct marks for these subroutine errors:

❌ Calling a Function like a Procedure: You cannot write CALL AddNumbers(10, 5). Because a function returns a value, that value must be caught by a variable: Answer ← AddNumbers(10, 5).

❌ Forgetting the RETURN keyword: If you declare a FUNCTION, you must end it with a RETURN [Variable] statement before the ENDFUNCTION keyword. Our PseudoStudio compiler will throw an error if you forget this!

Interactive Exam Practice

Scenario: Write a FUNCTION named GetDouble that takes a single INTEGER parameter by value, multiplies it by 2, and returns the result.

Hover or tap the black box to reveal the examiner's solution. Click Run Code to test it instantly!

Tap to Reveal Answer
FUNCTION GetDouble(Num : INTEGER) RETURNS INTEGER
    DECLARE Result : INTEGER
    Result ← Num * 2
    RETURN Result
ENDFUNCTION

// Test the function in the main program
OUTPUT "Double of 5 is: ", GetDouble(5)

Previous Topic

← Guide 4: Arrays

Back to Hub

All Guides ➔