Look at the subprogram below.
function calc(a, b)
if a > b then
return a - b
else
return a * b
endif
endfunction(a) State the output of: (i) print(calc(9, 4)) [1] (ii) print(calc(3, 5)) [1]
(b) Identify one parameter in the subprogram. [1]
(c) Give one difference between a function and a procedure. [1]
(d) Give one benefit of using local variables in subprograms. [1]
- 5(1)
- 15(1)
- a or b(1)
- A function returns a value; a procedure does not (accept the reverse statement)(1)
- e.g. they only exist while the subprogram runs, saving memory / they cannot be changed accidentally by other parts of the program / the same name can be reused elsewhere(1)
- Answer: (a) 5, 15; (b) a; (c) a function returns a value; (d) cannot be accidentally changed elsewhere
- Examiner tip: In (c), 'a function is shorter' doesn't score. The difference examiners want is that a function returns a value.