Guide 5 of 7

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.

⚡ Quick Review: Subroutines

  • Procedure: A block of code that performs an action (like printing text) but does not return a value to the main program.
  • Function: A block of code that performs a calculation and always returns a single value to the main program.
  • BYVAL (By Value): Passes a safe copy of a variable. The original data cannot be altered by the subroutine.
  • BYREF (By Reference): Passes a memory pointer. If the subroutine changes it, the original variable is permanently changed.

Exam Strategy: Top-Down Design

Examiners award high marks for Top-Down Design (step-wise refinement). This is the process of breaking a large algorithm down into smaller, manageable subroutines. By using Functions and Procedures, you prove to the examiner that you understand modular programming.

Python vs. Pseudocode Subroutines

If you code in Python, you use the def keyword to create every single subroutine. However, 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) 📘 Standard 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 array) 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 using the RETURN keyword.

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! Arrays are almost always passed BYREF.

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)