Programming and Software Development: Question 8
Syllabus 11.3
A mail-order company calculates the postage cost for a parcel using its weight in grams. The
FUNCTION below returns the correct postage cost, and the main program then adds together the
postage for two separate parcels.
FUNCTION CalculatePostage(WeightGrams : INTEGER) RETURNS REAL
DECLARE Cost : REAL
IF WeightGrams <= 100
THEN
Cost ← 1.50
ELSE
IF WeightGrams <= 500
THEN
Cost ← 2.75
ELSE
Cost ← 4.20
ENDIF
ENDIF
RETURN Cost
ENDFUNCTION
// Main program
DECLARE TotalCost : REAL
TotalCost ← CalculatePostage(80) + CalculatePostage(650)
OUTPUT "Total postage: ", TotalCost
(a) State the value returned by the call CalculatePostage(80), and identify which branch of
the nested IF statement inside CalculatePostage produces this value. [2]
(b) State the value returned by the call CalculatePostage(650), and identify which branch of
the nested IF statement inside CalculatePostage produces this value. [2]
(c) State the value of TotalCost output by the final line of the main program, showing your
working. [2]
(d) Explain why CalculatePostage must be written as a FUNCTION rather than as a
PROCEDURE for the line TotalCost ← CalculatePostage(80) + CalculatePostage(650) to work
correctly. [1]
Show worked solution Hide worked solution
Worked solution
Part (a): Tracing CalculatePostage(80)
WeightGrams = 80. The outer IF checks WeightGrams <= 100: 80 <= 100 is true, so the
THEN branch of the outer IF runs, setting Cost ← 1.50. The inner IF is never reached.
RETURN Cost then returns 1.50. [2 marks]: [1] for the correct returned value
(1.50), [1] for correctly identifying that it comes from the outer IF’s WeightGrams <= 100
branch.
Part (b): Tracing CalculatePostage(650)
WeightGrams = 650. The outer IF checks WeightGrams <= 100: 650 <= 100 is false, so the
ELSE branch runs, reaching the inner IF. This checks WeightGrams <= 500: 650 <= 500 is
also false, so the inner ELSE runs, setting Cost ← 4.20. RETURN Cost then returns 4.20.
[2 marks]: [1] for the correct returned value (4.20), [1] for correctly identifying
that it comes from the innermost ELSE branch (both WeightGrams <= 100 and
WeightGrams <= 500 are false).
Part (c): Calculating TotalCost
TotalCost ← CalculatePostage(80) + CalculatePostage(650)
← 1.50 + 4.20
← 5.70
So the final line outputs:
Total postage: 5.70
[2 marks]: [1] for correctly adding the two returned values (1.50 and 4.20), [1] for the correct final total, 5.70.
Part (d): Why a FUNCTION is required
A FUNCTION executes a block of code and then uses RETURN to send a single value back to the
point where it was called, so that value can be used immediately inside an expression. Exactly
what happens in CalculatePostage(80) + CalculatePostage(650), where both returned values are
added together. A PROCEDURE, by contrast, does not return any value to the caller at all; it can
only be invoked on its own using CALL, not embedded inside an arithmetic expression. If
CalculatePostage were written as a PROCEDURE, the line TotalCost ← CalculatePostage(80) + CalculatePostage(650) would not be valid, since there would be no returned values for the +
operator to add together. [1 mark].
Final answers
- (a)
CalculatePostage(80)returns1.50, from the outerIF’sWeightGrams <= 100branch. - (b)
CalculatePostage(650)returns4.20, from the innermostELSEbranch. - (c)
TotalCost = 5.70, output asTotal postage: 5.70. - (d) A
FUNCTIONmust be used because only aFUNCTIONreturns a value viaRETURNthat can be used inside an expression; aPROCEDUREreturns no value at all.