Skip to content
MathsGenie logo
Quick links
Open app

Course home

  1. A Level
  2. Computer Science OCR
  3. Revision guides

Subroutines, arrays, file handling and comments

What you'll learn

  • How to write and call functions and procedures in OCR pseudocode.
  • How byVal and byRef parameter passing affect variables.
  • How to declare and use 0-based arrays, including 2D arrays.
  • How to read from files, write to files, and add useful comments.

Before you start: tiny OCR pseudocode reminders

OCR pseudocode uses = for assignment, meaning “store this value in this variable”. For example, x = 10 stores 10 in x.

A main program is the top-level sequence of instructions that runs first. It can call named blocks of code called subroutines.

print() outputs a value, for example:

print("Hello World")

Subroutines

Definition

Subroutine

A subroutine is a named block of code that can be called from elsewhere in a program. It helps break a problem into smaller, reusable parts.

Subroutines support decomposition, which means splitting a large problem into smaller problems. They also make programs easier to test, maintain and reuse.

There are two main OCR pseudocode subroutines you need here: functions and procedures.

Functions: subroutines that return a value

Definition

Function

A function is a subroutine that sends a value back to the part of the program that called it. The value sent back is called the return value.

OCR pseudocode uses function, return and endfunction:

function triple(number)
    return number*3
endfunction

Called from the main program:

y = triple(7)

The function call triple(7) produces a value, so it can be used on the right-hand side of an assignment.

Example

Tracing a function call

For this code:

function triple(number)
    return number*3
endfunction

y = triple(7)
  1. The argument 7 is passed into the parameter number, so inside the function number has the value 7.
  2. The expression number*3 becomes 7*3, so the function returns 21.
  3. The call triple(7) is replaced by the returned value 21, so y = triple(7) stores 21 in y.

Procedures: subroutines that do not return a value

Definition

Procedure

A procedure is a subroutine that performs one or more instructions but does not return a value to the caller.

OCR pseudocode uses procedure and endprocedure:

procedure greeting(name)
    print("hello"+name)
endprocedure

Called from the main program:

greeting("Hamish")

Here, name receives "Hamish" and the procedure prints a greeting. The + joins the two strings together.

Key Idea

Function or procedure?

  • Use a function when the subroutine must calculate or produce a value.
  • Use a procedure when the subroutine performs an action, such as printing, updating a file, or changing a value passed by reference.

Parameters and arguments

A parameter is the named variable in the subroutine heading, such as number in function triple(number).

An argument is the actual value supplied when the subroutine is called, such as 7 in triple(7).

So in:

y = triple(7)

number is the parameter, and 7 is the argument.

Passing by value and passing by reference

By default in OCR pseudocode, values passed to subroutines are assumed to be passed by value unless the question states otherwise.

Definition

byVal and byRef

byVal means the subroutine receives a copy of the value. byRef means the subroutine receives a reference to the original variable, so changes inside the subroutine affect the caller’s variable.

If the distinction matters, OCR pseudocode will use byVal and byRef in the subroutine heading:

procedure foobar(x:byVal, y:byRef)
    ...
endprocedure

In this example, x is passed by value and y is passed by reference.

The diagram shows why this matters: changing a by-value parameter changes only the copy, while changing a by-reference parameter changes the original variable.

Diagram comparing pass by value and pass by reference

Example

Tracing by value and by reference

For this code:

procedure update(a:byVal, b:byRef)
    a = a + 10
    b = b + 10
endprocedure

x = 5
y = 5
update(x, y)
print(x)
print(y)
  1. When update(x, y) is called, a receives a copy of x, so a starts as 5. b refers to the original variable y.
  2. a = a + 10 changes the local copy a to 15, but x is still 5.
  3. b = b + 10 changes the original variable y because b is passed by reference, so y becomes 15.
  4. After the procedure ends, a is discarded. The program prints 5, then 15.
Common Mistake

Assuming parameters always change the original variable

Unless byRef is shown, assume parameters are passed by value. Changing the parameter inside the subroutine will not change the original variable in the main program.

Arrays

Definition

Array

An array is a data structure that stores multiple values under one name, with each value accessed using an index.

OCR arrays are 0-based, meaning the first element is at index 0, not index 1. The keyword array is used to declare one.

The diagram shows a 1D array and a 2D array using OCR-style indexing.

Diagram showing 0-based 1D and 2D array indexing

1D arrays

A 1D array is a single row of values.

array names[5]
names[0] = "Ahmad"
names[1] = "Ben"
names[2] = "Catherine"
names[3] = "Dana"
names[4] = "Elijah"

print(names[3])

This prints:

Dana

The declaration array names[5] creates five elements, with valid indexes 0, 1, 2, 3 and 4.

Common Mistake

Off-by-one array access

In array names[5], names[5] is not valid. The array has five elements, but because indexing starts at 0, the final valid index is 4.

Processing an array with a loop

A traversal means visiting each element in a data structure, usually to search, count, total or compare values.

array scores[4]
scores[0] = 12
scores[1] = 19
scores[2] = 15
scores[3] = 20

highest = scores[0]

for i = 1 to 3
    if scores[i] > highest
        highest = scores[i]
    endif
next

print(highest)
Example

Finding the largest value in an array

  1. highest = scores[0] sets the starting best value to 12, so the algorithm begins with a real value from the array.
  2. When i = 1, scores[1] is 19, which is greater than 12, so highest becomes 19. When i = 2, scores[2] is 15, which is not greater than 19, so highest stays 19.
  3. When i = 3, scores[3] is 20, which is greater than 19, so highest becomes 20.
  4. The loop ends after index 3, and print(highest) outputs 20.

2D arrays

A 2D array stores values using two indexes, often thought of as row and column.

array board[8,8]
board[0,0] = "rook"

This creates an 8 by 8 grid. Valid row indexes are 0 to 7, and valid column indexes are 0 to 7. The element board[0,0] is the first row and first column.

A 2D array is useful for grids, maps, game boards, timetables and tables of values.

Tip

Reading 2D indexes

In these notes, treat board[row,column] as row first, column second. If a question defines a different convention, follow the question carefully.

Reading from files

A file stores data outside the program, usually on secondary storage. In OCR pseudocode, you first open the file, then read or write lines, then close the file.

To open a file for reading, use openRead.

To read one line, use readLine.

myFile = openRead("sample.txt")
x = myFile.readLine()
myFile.close()

This makes x store the first line of sample.txt.

Reading until the end of a file

endOfFile() checks whether the file has reached the end. It returns a Boolean value: true or false.

The OCR pattern for reading every line is:

myFile = openRead("sample.txt")

while NOT myFile.endOfFile()
    print(myFile.readLine())
endwhile

myFile.close()

NOT myFile.endOfFile() means “keep looping while we have not reached the end of the file”.

Example

Counting lines in a file

Suppose sample.txt contains three lines. This program counts them:

count = 0
myFile = openRead("sample.txt")

while NOT myFile.endOfFile()
    line = myFile.readLine()
    count = count + 1
endwhile

myFile.close()
print(count)
  1. Before the first read, the file has unread data, so NOT myFile.endOfFile() is true. The first line is read into line, and count becomes 1.
  2. The loop repeats for the second and third lines. Each successful readLine() reads exactly one line, so count becomes 2 and then 3.
  3. After the third line has been read, the next condition check finds the end of the file. The loop stops, the file is closed, and the program prints 3.

Writing to files

To open a file for writing, use openWrite.

To write a line, use writeLine.

myFile = openWrite("sample.txt")
myFile.writeLine("Hello World")
myFile.close()

This makes "Hello World" the contents of sample.txt.

Common Mistake

openWrite overwrites

openWrite overwrites any previous contents of the file. OCR’s guide here does not give an append mode, so do not invent one unless the question explicitly defines it.

Tip

Open, process, close

A safe file-handling pattern is: open the file once, read or write the required lines, then close it once at the end.

Comments

Definition

Comment

A comment is text in the program that is ignored when the program runs. It is written for humans reading the code.

OCR pseudocode uses // for comments:

print("Hello World") //This is a comment

A comment can explain the purpose of a section, clarify an assumption, or make a tricky line easier to understand.

Good comments explain why something is being done, not just what the code already says.

total = total + price // add current item to the running total

Comments do not change the output, the variables, or the flow of the program.

Common Mistake

Using comments instead of code

A comment does not execute. If the algorithm needs to read a file, update a variable, or call a subroutine, you must write pseudocode instructions as well as any comments.

Exam technique

In the exam

  1. For subroutines, identify whether it is a function or a procedure. Replace a function call with its returned value when tracing.
  2. For parameters, assume byVal unless byRef is shown. Track by-reference changes back to the original variable.
  3. For arrays and files, be exact: arrays are 0-based, file reading usually uses while NOT file.endOfFile(), and openWrite overwrites existing contents.
Self review

Check yourself

  • In array grid[3,4], what are the valid row and column indexes?
  • What is the difference between a parameter and an argument?
  • Why does changing a byRef parameter affect the original variable, but changing a byVal parameter usually does not?
PreviousNext

How was this guide?

Teach Genie

Review Subroutines, arrays, file handling and comments by teaching Genie

Teach it back in your own words, spot gaps, and remember it better.

Start teaching
Genie and Baby Genie

Lesson

Recap your knowledge with an interactive lesson

7 minute activity

Start lesson

A subroutine is a named block of code that you can call from somewhere else in the program. Subroutines support decomposition because a large task is split into smaller parts that are easier to test, reuse and fix.

A function returns a value with return, so a call such as y = triple(7) can store the answer. A procedure performs an action such as printing or updating data, but it does not return a value.

A good rule is simple: if you need a result back, use a function. If you need something done, use a procedure, and remember that the main program is the top-level code that runs first.

Flashcards

Remember key concepts with flashcards

23 flashcards

Practice flashcards

In OCR pseudocode, what does x = 10 do?

Subroutines, arrays, file handling and comments Revision Guide

  1. A Level
  2. /Computer Science
  3. /Subroutines, arrays, file handling and comments

Revision guides