Procedures, Functions, Arrays and File Handling: Question 2

Syllabus 8.1

Structured 5 marks

A zoo uses a function, EntryFee, to calculate the entry price, in dollars, for one visitor of a given age, Age. The rules are:

  • age under 3: a fee of 0.00
  • age 3 to 12 inclusive: a fee of 5.50
  • age 13 and over: a fee of 9.75

Part of the pseudocode for this function is shown below, with one line missing.

FUNCTION EntryFee(Age : INTEGER) RETURNS REAL
    DECLARE Fee : REAL
    IF Age < 3
        THEN
            Fee ← 0.00
        ELSE
            IF Age <= 12
                THEN
                    Fee ← 5.50
                ELSE
                    Fee ← 9.75
            ENDIF
    ENDIF
    // missing line (i)
ENDFUNCTION

(a) State the missing line of pseudocode, labelled (i), that must appear immediately before ENDFUNCTION so the function correctly sends back the value it has calculated. [1]

(b) The main program declares a global variable, TotalTaken, and calls the function twice, as shown below.

DECLARE TotalTaken : REAL
TotalTaken ← 0
TotalTaken ← TotalTaken + EntryFee(8)
TotalTaken ← TotalTaken + EntryFee(15)
OUTPUT TotalTaken

Trace this code and state the value output for TotalTaken. [2]

(c) Age and Fee are both local to the function EntryFee, while TotalTaken is global. Describe the difference between a local variable and a global variable, referring to Fee and TotalTaken in your answer. [2]

Show worked solution Hide worked solution

Worked solution

Part (a): Completing the function with RETURN

A function must send its calculated result back to whichever line called it, and in Cambridge pseudocode this is always done with a RETURN statement, never OUTPUT. Since Fee holds the value worked out by the IF statement, the missing line is:

RETURN Fee

Part (b): Tracing two calls to EntryFee

Trace TotalTaken ← TotalTaken + EntryFee(8):

  • Age ← 8. Is 8 < 3? No. Is 8 <= 12? Yes, so Fee ← 5.50, and the function returns 5.50.
  • TotalTaken ← 0 + 5.50 = 5.50.

Trace TotalTaken ← TotalTaken + EntryFee(15):

  • Age ← 15. Is 15 < 3? No. Is 15 <= 12? No, so Fee ← 9.75, and the function returns 9.75.
  • TotalTaken ← 5.50 + 9.75 = 15.25.

OUTPUT TotalTaken displays 15.25.

Part (c): Local versus global variables

Fee (and the parameter Age) are local to EntryFee: a fresh copy is created every time the function is called, it can only be referred to from inside that function, and it is destroyed the moment the function returns. It does not remember its value from one call to the next (the second call to EntryFee starts with a brand-new Fee, not the 5.50 left over from the first call).

TotalTaken is global: it is declared once, outside every procedure and function, at the top of the program. It keeps its value for the entire run of the program, and it can be read and changed from anywhere, including from inside a function, which is exactly why TotalTaken can correctly accumulate the results of both calls to EntryFee above.

Final answers

  • (a) RETURN Fee
  • (b) TotalTaken = 15.25
  • (c) A local variable (Fee) is created and destroyed within a single call to its function; a global variable (TotalTaken) exists for the whole program and can be used or updated by any part of it, including functions.