Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 4% of your exam marks.
Writing and calling procedures/functions with parameters is tested in Paper 2.
A procedure is a named block of code that performs a task without returning a value. It is invoked using the
CALLkeyword.
A procedure with no parameters:
PROCEDURE ProcedureName
// statements that do the work
ENDPROCEDURE
A procedure with parameters (values passed in):
PROCEDURE ProcedureName(Param1 : TYPE, Param2 : TYPE, ...)
// statements that use the parameters
ENDPROCEDURE
Each parameter is named and given a data type, so the procedure knows what kind of value to expect.
Procedures are run using the CALL keyword:
CALL ProcedureName
CALL ProcedureName(Value1, Value2)
The values in brackets are called arguments. Each argument is matched in order to the corresponding parameter in the procedure's definition.
Example — calculate-area procedure
PROCEDURE CalculateArea(Length : INTEGER, Width : INTEGER)
Area ← Length * Width
OUTPUT "The area is ", Area
ENDPROCEDURE
// Main program
CALL CalculateArea(5, 3)
When CALL CalculateArea(5, 3) is executed:
Length = 5 and Width = 3.Area = 5 * 3 = 15.The area is 15.CALL.The procedure does its job (outputting the area), but it does not give a value back for the caller to use. The caller cannot say X ← CalculateArea(5, 3).
Choose a procedure when the sub-program's job is to do something rather than to compute and return something: