Look at this subroutine.
SUBROUTINE calcPrice(price, member)
IF member = True THEN
RETURN price * 0.9
ELSE
RETURN price
ENDIF
ENDSUBROUTINE(a) State one parameter used in the subroutine. [1]
(b) State the value returned by calcPrice(40, True). [1]
(c) Explain the difference between a local variable and a global variable. [2]
(d) Give two advantages of using subroutines in a program. [2]
- (a) price / member(1)
- (b) 36 (accept 36.0)(1)
- (c) A local variable can only be accessed within the subroutine where it is declared; allow it only exists while the subroutine is running(1)
- (c) A global variable can be accessed from anywhere in the program (all subroutines); allow it exists for the whole time the program runs(1)
- (d) First advantage, any one from: code can be reused / called many times without rewriting it; programs are easier to test / debug (each subroutine tested separately); programs are easier to read / understand / maintain; work can be split between programmers; makes decomposition of the problem easier(1)
- (d) A second, different advantage from the list above(1)
- Answer: (a) price (b) 36 (c) local: only in the subroutine; global: whole program (d) e.g. reuse, easier testing
- Examiner tip: In (c) you need both halves of the comparison for 2 marks. Describing only local variables gets 1.