Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 1 of 3.
Declare each variable with its data type. A holds a whole number and B holds text:
DECLARE A : INTEGER DECLARE B : STRING
DECLARE A : INTEGER [1]
DECLARE B : STRING [1]
Define the function first. It needs a parameter X and it returns text. Use MOD to test for odd or even: an even number has remainder 0 when divided by 2.
FUNCTION Odds(X : INTEGER) RETURNS STRING
IF MOD(X, 2) = 0
THEN
RETURN "Even"
ELSE
RETURN "Odd"
ENDIF
ENDFUNCTION
B ← Odds(A)Uses FUNCTION, ENDFUNCTION and RETURNS STRING [1]
Function is named Odds [1]
Parameter defined correctly, for example X : INTEGER [1]
Odd or even tested using MOD (or DIV or ROUND) on the parameter [1]
Returns "Odd" and "Even" correctly [1]
Function call stores the result in B: B ← Odds(A) [1]
▶ Topic lesson
A lesson on this topic · by Mr Bulmer's Learning Zone. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.