Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 4% of your exam marks.
Writing and calling procedures/functions with parameters is tested in Paper 2.
Example — A cinema charges £8.50 for a standard seat and £14.00 for a premium seat.
Write a function TicketPrice that takes the size of the group and the chosen seat type as inputs, then returns the total amount to pay.
FUNCTION TicketPrice(GroupSize : INTEGER, SeatType : STRING) RETURNS REAL
DECLARE CostPerSeat : REAL
IF SeatType = "premium" THEN
CostPerSeat ← 14.00
ELSE
CostPerSeat ← 8.50
ENDIF
RETURN GroupSize * CostPerSeat
ENDFUNCTION
// Using the function
OUTPUT TicketPrice(2, "standard") // outputs 17.00
OUTPUT TicketPrice(3, "premium") // outputs 42.00
Key features of this answer that earn marks:
FUNCTION ... RETURNS REAL (returns a number with a decimal part).GroupSize and SeatType are declared with data types.CostPerSeat is used to hold the looked-up price.RETURN statement gives back the calculated total.OUTPUT), not with CALL.The same calculation written as a procedure would not return a value; instead it would output the cost directly. The function version is more useful because the caller can do other things with the returned value (display it, save it, add a booking fee, etc.).