AS · Practice questions · Procedures and functions

Small parts, clear whole.

Six original Cambridge-style questions on procedures, functions and their terminology, with mark points.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Statement or expression.

01
Recall
[3 marks]

Explain what is meant by each term: (a) parameter (b) argument (c) return value.

  • (a) An identifier in the procedure or function header that receives a value passed to the subroutine. ✓
  • (b) The actual value (or variable) passed to the subroutine when it is called. ✓
  • (c) The single value a function sends back to the calling code, which replaces the function call in the expression. ✓
02
Explain
[3 marks]

A large program is being written by a team. Explain three benefits of writing it as a set of procedures and functions.

  • Each subroutine can be written and tested separately, possibly by different programmers. ✓
  • Code that is needed in several places is written once and called many times, so the program is shorter. ✓
  • Maintenance is easier: a change or fix is made in one subroutine only; subroutines can also be reused in other programs (any three). ✓
03
Trace
[4 marks]

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.

  • N 4073, Result 3; N 407, Result 10; N 40, Result 10; N 4, Result 14; N 0, loop ends. ✓
  • Mystery(4073) returns 14. ✓
  • (b) 14 * 2 = 28 is output. ✓
  • (c) It returns the sum of the digits of N. ✓
04
Write
[4 marks]

Write a function MaxOfThree that takes three integer parameters and returns the largest.

  • Header: 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. ✓
05
Write
[4 marks]

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). ✓
06
Analysis
[3 marks]

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.

  • Use a conditional loop (WHILE / REPEAT) that stops when the code is found or the end of the array is reached, or RETURN TRUE as soon as a match is found. ✓
  • Fewer comparisons are made when the code is found early, for example 1 instead of 500 if it is the first element. ✓
  • So the function runs faster on average; it still returns FALSE after checking all 500 if the code is not there. ✓

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.