Arrays, subroutines and files
IGCSE Computer Science · Programming
Arrays
An array stores many values of the same data type under one name, each reached by an index.
A one-dimensional array is a list: $\text{Marks}[1]$, $\text{Marks}[2]$, … A two-dimensional array is a table with rows and columns: $\text{Grid}[3,5]$ means row $3$, column $5$.
Why arrays matter: without one, storing $100$ marks needs $100$ separately named variables, and there is no way to process them in a loop. With an array, one loop handles all of them:
$\textbf{FOR } i \leftarrow 1 \textbf{ TO } 100$
$\quad \text{total} \leftarrow \text{total} + \text{Marks}[i]$
$\textbf{NEXT } i$
That is the real reason arrays exist: they make the index a variable, so a loop can walk through the data.
Procedures and functions
| procedure | a named block of code that carries out a task and does not return a value |
| function | a named block of code that returns a single value to the point where it was called |
The distinction is exactly that: a function returns a value; a procedure does not.
Because a function returns a value, it can be used inside an expression: $\text{x} \leftarrow \text{Square}(5) + 2$. A procedure cannot — it is called as a statement on its own.
A parameter is a value passed into a subroutine so it can work on different data each time. $\text{Square}(5)$ and $\text{Square}(9)$ use the same code with a different parameter.
Why subroutines are worth using
- reuse — write once, call from many places, instead of copying the code;
- easier testing — each subroutine is tested on its own;
- easier maintenance — a change is made in one place, not in every copy;
- shorter, clearer programs — the main program reads as a list of named steps;
- teamwork — different people write different subroutines at the same time.
Library routines are ready-written subroutines supplied with the language — $\text{ROUND}$, $\text{LENGTH}$, $\text{RANDOM}$. They save time and are already tested and reliable.
File handling
Programs store data in files so it survives after the program closes. Without a file, everything in memory is lost.
The sequence is always the same:
- open the file, stating whether to read or write;
- read from it or write to it;
- close it.
Closing matters more than it looks. Data being written is held in a temporary buffer in memory and only pushed out to disk when the file is closed. Forgetting to close means the data may never reach the disk — and again, nothing reports this.
Closing also releases the file so other programs can use it.
Worked example: why use an array instead of many separate variables
Explain why a program handling the marks of $100$ students should use an array rather than $100$ individual variables, and state the difference between a one-dimensional array and a two-dimensional array.
Solution
The problem with $100$ individual variables.
Difficulty one — you must declare $100$ names. The programmer has to write $\text{mark1}$, $\text{mark2}$, … up to $\text{mark100}$. The declarations alone take $100$ lines.
Difficulty two, and this is the fatal one — you cannot use a loop.
To add them up, you must write $100$ terms: $\text{total} \leftarrow \text{mark1} + \text{mark2} + \dots$
A loop cannot help, because each variable has a different name and there is no way for a loop to “guess” the next variable's name.
Difficulty three — it does not scale. If next year there are $120$ students, you have to change everywhere in the program.
How an array solves this.
An array stores everything under one single name, and each value is accessed by an index: $\text{Marks}[1]$, $\text{Marks}[2]$, …
The key point: the index can be a variable. This allows a single loop to go through all the data:
$\textbf{FOR } i \leftarrow 1 \textbf{ TO } 100$
$\quad \text{total} \leftarrow \text{total} + \text{Marks}[i]$
$\textbf{NEXT } i$
Three lines in place of $100$ lines. And to change to $120$ students you only change one number.
That is the real reason arrays exist: they make the index a variable.
One-dimensional and two-dimensional arrays.
A one-dimensional array is a list — it needs only one index: $\text{Marks}[7]$ is the mark of the $7$th student.
A two-dimensional array is a table with rows and columns — it needs two indices: $\text{Marks}[7, 3]$ is the mark of the $7$th student in the $3$rd subject.
When you need the two-dimensional type: when each element has several related values. The marks of $100$ students in $5$ subjects form a $100 \times 5$ table, and a two-dimensional array describes it far more neatly than five separate one-dimensional arrays.
Traversing a two-dimensional array needs two nested loops: the outer loop goes along the rows, the inner loop along the columns.
Worked example: procedures, functions and closing files
(a) State the difference between a procedure and a function, with examples. (b) State three benefits of using subroutines. (c) Describe the three steps of working with files and explain why the closing step is important.
Solution
(a) Procedures and functions.
A procedure is a named block of statements that performs a task and then returns no value.
Example: $\text{DrawFrame}()$ — it draws the frame and stops. There is no result to bring back.
A function is a named block of statements that returns a value to the place that called it.
Example: $\text{Square}(5)$ returns $25$.
The practical consequence of this difference. Because a function returns a value, it can be used inside an expression:
$\text{x} \leftarrow \text{Square}(5) + 2$
A procedure cannot do this — it is called as a statement on its own line, because there is no value to put into the expression.
Both take parameters: values passed in so that the subroutine works on different data each time.
(b) Three benefits of subroutines.
Reuse. Write once, call from many places. There is no need to copy the same piece of code again and again.
Easier maintenance. This is the most important consequence of reuse: when a change is needed, you change one place. If the code is copied into $10$ places you must remember to change all $10$ — and missing just one creates a fault.
Easier testing. Each subroutine is tested separately, so when there is a fault we know straight away which part it is in.
(Two more benefits: the main program is shorter and clearer because it reads like a list of named steps; and several people can work in parallel on different subroutines.)
(c) The three steps of working with files.
Step 1 — open the file, stating clearly whether it is opened for reading or for writing.
Step 2 — read data from the file, or write data to the file.
Step 3 — close the file.
Why the closing step is important.
Reason one — the data may not yet be on disk. When a program writes, the data does not go straight to disk. It sits temporarily in a buffer in memory, and is only pushed to disk when the file is closed.
Forgetting to close means the data in the buffer may be lost entirely when the program ends.
What makes it dangerous: the program finishes completely normally, with no error message at all. Only when you open the file do you discover it is empty or missing data.
Reason two — releasing the file. An open file is often locked, so other programs cannot use it. Closing the file hands it back to the system.