Six original Cambridge-style questions on procedures, functions and their terminology, with mark points.
PROCEDURE Name(...) ... ENDPROCEDURE; run with CALL Name(...).RETURNS type; uses RETURN; used inside an expression, never with CALL.Explain what is meant by each term: (a) parameter (b) argument (c) return value.
A large program is being written by a team. Explain three benefits of writing it as a set of procedures and functions.
Study this function.
FUNCTION Mystery(N : INTEGER) RETURNS INTEGER
DECLARE Result : INTEGER
Result ← 0
WHILE N > 0
Result ← Result + (N MOD 10)
N ← N DIV 10
ENDWHILE
RETURN Result
ENDFUNCTION(a) Trace Mystery(4073). (b) State the output of OUTPUT Mystery(4073) * 2. (c) Describe the purpose of the function.
Write a function MaxOfThree that takes three integer parameters and returns the largest.
FUNCTION MaxOfThree(A : INTEGER, B : INTEGER, C : INTEGER) RETURNS INTEGER. ✓Max ← A, then IF B > Max THEN Max ← B ENDIF. ✓IF C > Max THEN Max ← C ENDIF. ✓RETURN Max and ENDFUNCTION. ✓Write a procedure TimesTable that takes an integer parameter and outputs its multiplication table from 1 times to 12 times, one line each, for example 7 x 3 = 21. Then write a statement that uses it to output the 7 times table.
PROCEDURE TimesTable(Number : INTEGER) ... ENDPROCEDURE, with Count declared locally. ✓FOR Count ← 1 TO 12 ... NEXT Count. ✓OUTPUT Number, " x ", Count, " = ", Number * Count inside the loop. ✓CALL TimesTable(7). ✓A function checks whether a code is in an array of 500 codes. It uses a FOR loop from 1 to 500, sets Found ← TRUE when the code matches, and returns Found after the loop. Explain how the function could be made more efficient, and why this is better.
Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.