Functions
Functions are slightly different because they will return a value to another part of the program. It is therefore necessary to specify the type of data that a function will return. This can be seen beside the parameter/argument list.
A function also requires a return statement at the end of the function. This statement identifies the local variable used to return a value once the algorithm has been executed.
FUNCTION FindMax (REAL testscore[ ]) RETURNS REAL
SET maximum TO testscore [0]
FOR counter FROM 1 TO 9 DO
IF testscore[counter] > maximum THEN
SET maximum TO testscore[counter]
END IF
END FOR
RETURN maximum
END FUNCTION
While indexing will usually start at 0, the loop within this function indexes from 1. This is because the previous line already assigned the value held in index position 0 to the maximum variable, so it would be unnecessary to iterate over this index location again.
When called, this function would return the value of the local variable called maximum.
Calling a function
The main program may contain a variable called 鈥榟ighest鈥. It is possible to use the function to assign a value to this variable. The value that the function returns is the value that will be assigned to the 鈥榟ighest鈥 variable.
SET highest TO FindMax (testscore [ ])
There are three functions to know when reading Reference Language questions. They are:
- Random - will return a random number when executed.
- Length - will return the number of characters found in a string value
- Round - will return a rounded version of a number, ie it may round to two decimal places when a value it uses has actually had seven decimal places to start with.