SUBROUTINE power(base, exp) result ← 1 FOR i ← 1 TO exp result ← result * base ENDFOR RETURN result ENDSUBROUTINE
OUTPUT area(5, 3) ← 15 OUTPUT power(2, 4) ← 16
Exam Practice
Have a go at this question
AQA-style question
Write a function called isPass that takes a score as a parameter and returns True if the score is 50 or more, and False otherwise. Show how to call it and output the result for a score of 65.
5 marks
SUBROUTINE isPass(score) IF score >= 50 THEN RETURN True ELSE RETURN False ENDIF ENDSUBROUTINE
result ← isPass(65) OUTPUT result ← True
Key Takeaways
What to Remember
Parameters = inputs to a subroutine; Arguments = values passed in the call
RETURN — sends a value back; subroutines with RETURN act as functions
Use the returned value: result ← myFunction(args)
Procedures perform actions; functions calculate and return a result