Procedures, Functions, Arrays and File Handling: Question 6

Syllabus 8.1

Structured 5 marks

An online clothing store uses a procedure, ShowDiscountedPrice, to work out and display the discounted price of an item, given its original price and a discount percentage.

PROCEDURE ShowDiscountedPrice(ItemPrice : REAL, DiscountPercent : INTEGER)
    DECLARE Discounted : REAL
    Discounted ← ItemPrice - (ItemPrice * DiscountPercent / 100)
    OUTPUT Discounted
ENDPROCEDURE

CALL ShowDiscountedPrice(40.00, 25)

(a) State the value that the parameter ItemPrice holds, and the value that the parameter DiscountPercent holds, during this call. [1]

(b) Trace the procedure for this call, showing your working, and state the value output. [2]

(c) A second call is written, by mistake, with the two arguments the wrong way round: CALL ShowDiscountedPrice(25, 40). State the value that would be output by this call, showing your working, and explain why it is different from the value in part (b), even though the same two numbers, 25 and 40, are used in both calls. [2]

Show worked solution Hide worked solution

Worked solution

Part (a): Matching arguments to parameters by position

Parameters are matched to the arguments in a CALL statement by position, not by name or size. In CALL ShowDiscountedPrice(40.00, 25), the first argument, 40.00, is copied into the first parameter, ItemPrice, and the second argument, 25, is copied into the second parameter, DiscountPercent.

So ItemPrice = 40.00 and DiscountPercent = 25. [1 mark]

Part (b): Tracing the procedure

Discounted ← ItemPrice - (ItemPrice * DiscountPercent / 100), with ItemPrice = 40.00 and DiscountPercent = 25:

  • ItemPrice * DiscountPercent = 40.00 * 25 = 1000
  • 1000 / 100 = 10.00 (this is the discount amount)
  • Discounted ← 40.00 - 10.00 = 30.00

OUTPUT Discounted displays 30.00. [2 marks]: [1] for correctly finding the discount amount, 10.00, [1] for the correct final output, 30.00.

Part (c): Tracing the call with the arguments swapped

CALL ShowDiscountedPrice(25, 40) matches arguments to parameters by position exactly as before, so this time ItemPrice = 25 and DiscountPercent = 40:

  • ItemPrice * DiscountPercent = 25 * 40 = 1000
  • 1000 / 100 = 10.00
  • Discounted ← 25 - 10.00 = 15

OUTPUT Discounted displays 15. [1 mark] for this value.

This is different from part (b) even though the discount amount removed is exactly the same value, 10.00, in both calls. That happens because multiplication is commutative (40.00 * 25 = 25 * 40 = 1000). The real difference is which number is treated as the original price to be reduced: in part (b) the price being discounted is 40.00 (giving 30.00), while in part (c) it is only 25 (giving 15). Swapping the arguments swaps which value plays the role of ItemPrice and which plays the role of DiscountPercent, so the two calls calculate two genuinely different things even though they share the same two numbers. [1 mark] for this explanation.

Final answers

  • (a) ItemPrice = 40.00, DiscountPercent = 25
  • (b) Output = 30.00
  • (c) Output = 15, different from (b) because swapping the arguments changes which value is the original price and which is the percentage discount