Topic 8 · Programming
Procedures, functions, and maintainable code
Write reusable subroutines with parameters, return values from functions, understand local and global variables, and keep code maintainable.
Why subroutines?
A subroutine is a named block of code that does one job and can be called from anywhere in the program. Writing it once and calling it many times makes programs shorter, easier to test, and easier to change.
There are two kinds. A procedure does a task. A function does a task and returns a value to where it was called.
Procedures
Define a procedure with PROCEDURE ... ENDPROCEDURE and run it with CALL. Values passed in are parameters.
PROCEDURE DrawLine(Size : INTEGER)
DECLARE Count : INTEGER
DECLARE Line : STRING
Line <- ""
FOR Count <- 1 TO Size
Line <- Line & "-"
NEXT Count
OUTPUT Line
ENDPROCEDURE
CALL DrawLine(10)
OUTPUT "Menu"
CALL DrawLine(10)Functions
A function states the type of value it returns with RETURNS, and sends the value back with RETURN. Because it gives back a value, you use a function inside an expression instead of with CALL.
- 1Call
Area(4.5, 3)is reached. - 2Parameters
Widthgets 4.5,Heightgets 3. - 3RunThe function body calculates 13.5.
- 4Return13.5 replaces the call and is stored in
RoomArea.
FUNCTION Area(Width : REAL, Height : REAL) RETURNS REAL
RETURN Width * Height
ENDFUNCTION
DECLARE RoomArea : REAL
RoomArea <- Area(4.5, 3)
OUTPUT "Carpet needed: ", RoomArea, " square metres"Local and global variables
A local variable is declared inside a subroutine. It exists only while the subroutine runs and cannot be used outside it. Count and Line in DrawLine are local.
A global variable is declared in the main program and can be used anywhere, including inside subroutines.
Exam tip. Prefer local variables and parameters. Subroutines that only use their own variables are easier to test and reuse, and cannot accidentally change other parts of the program.
Writing maintainable programs
Programs are read far more often than they are written. Make yours easy for someone else (or you, next month) to understand and change:
- Use meaningful identifiers:
TotalScoreinstead ofT. - Add comments with
//to explain why the code does something. - Use procedures and functions so each part does one clear job.
- Use constants for fixed values, and indent blocks consistently.
// Works out the cost of a cinema trip
CONSTANT TicketPrice <- 8.5
DECLARE People : INTEGER
INPUT People
OUTPUT "Total cost: ", People * TicketPrice // before any discountKey terms
- Procedure
- A subroutine that performs a task and does not return a value.
- Function
- A subroutine that returns a value.
- Parameter
- A value passed into a subroutine when it is called.
- Local variable
- A variable declared inside a subroutine and only usable there.
- Global variable
- A variable declared in the main program and usable anywhere.
Check yourself
What is the difference between a procedure and a function?Show answer
A function returns a value; a procedure does not.
Write the header of a function IsEven that takes an integer and returns a Boolean.Show answer
FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN.
Give two features that make a program maintainable.Show answer
For example: meaningful identifiers, comments, subroutines, constants, consistent indentation.