Procedures and functions
The definition and use of procedures and functions is explicitly required in the AS & A Level (9618) syllabus. Any pseudocode functions used in an examination will be defined.
Defining and calling procedures
A procedure with no parameters is defined as follows:
PROCEDURE <identifier>
<statement(s)>
ENDPROCEDUREA procedure with parameters is defined as follows:
PROCEDURE <identifier>(<param1> : <data type>, <param2> : <data type>...)
<statement(s)>
ENDPROCEDUREThe <identifier> is the identifier used to call the procedure. Where used, param1, param2 etc. are identifiers for the parameters of the procedure. These will be used as variables in the statements of the procedure.
Procedures defined as above should be called as follows, respectively:
CALL <identifier>
CALL <identifier>(Value1, Value2, ...)These calls are complete program statements.
When parameters are used, Value1, Value2... must be of the correct data type and in the same sequence as in the definition of the procedure.
Unless otherwise stated, it should be assumed that parameters are passed by value. (See section 8.3).
//Example – definition and use of procedures with and without parameters
PROCEDURE DefaultSquare
CALL Square(100)
ENDPROCEDURE
PROCEDURE Square(Size : INTEGER)
FOR Side ← 1 TO 4
CALL MoveForward(Size)
CALL Turn(90)
NEXT Side
ENDPROCEDURE
IF Size = Default THEN
CALL DefaultSquare
ELSE
CALL Square(Size)
ENDIFDefining and calling functions
Functions operate in a similar way to procedures, except that in addition they return a single value to the point
at which they are called. Their definition includes the data type of the value returned. A function with no parameters is defined as follows:
FUNCTION <identifier> RETURNS <data type>
<statement(s)>
ENDFUNCTIONA function with parameters is defined as follows:
FUNCTION <identifier>(<param1> : <data type>, <param2> : <data type>...) RETURNS <data type>
<statement(s)>
ENDFUNCTIONThe keyword RETURN is used as one of the statements within the body of the function to specify the value to be returned. Normally, this will be the last statement in the function definition, however, if the RETURN statement is in the body of the function its execution is immediate and any subsequent lines of code are omitted.
Because a function returns a value that is used when the function is called, function calls are not complete program statements. The keyword CALL should not be used when calling a function. Functions should only be called as part of an expression. When the RETURN statement is executed, the value returned replaces the function call in the expression and the expression is then evaluated.
//Example – definition and use of a function
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT "Penalty Fine = ", Max(10, Distance*2)def {function}({parameters}):
{statements}
return {value}def factorial(number):
result = 1
for i in range(1, number + 1):
result = result * i
return result
f1 = factorial(5)
f2 = factorial(10)
print(f1)
print(f2)Passing parameters by value or by reference
To specify whether a parameter is passed by value or by reference, the keywords BYVAL and BYREF precede the parameter in the definition of the procedure. If there are several parameters passed by the same method, the BYVAL or BYREF keyword need not be repeated.
//Example – passing parameters by reference
PROCEDURE SWAP(BYREF X : INTEGER, Y : INTEGER)
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDUREIf the method for passing parameters is not specified, passing by value is assumed. How this should be called and how it operates has already been explained in Section 8.1.
Parameters should not be passed by reference to a function.