SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
AQA 8525 · Section 3.2 · 3.2.10b

Parameters
& Return Values

Parameters · Arguments · RETURN · Functions vs Procedures

CSZoneAQA GCSE Computer Science 8525
Parameters

Passing Data into Subroutines

WITH PARAMETERS
SUBROUTINE greet(name)
  OUTPUT 'Hello, ' + name
ENDSUBROUTINE

greet('Ahmed')
← Hello, Ahmed
greet('Beth')
← Hello, Beth
KEY TERMS
Parameter — the variable name in the definition (e.g. name)
Argument — the actual value passed in when calling (e.g. 'Ahmed')
Functions with RETURN

Functions Return a Value

FUNCTION DEFINITION
SUBROUTINE square(n)
  RETURN n * n
ENDSUBROUTINE

SUBROUTINE addTax(price)
  RETURN price * 1.2
ENDSUBROUTINE
CALLING WITH RETURN
result ← square(5)
OUTPUT result
← 25

total ← addTax(10.00)
OUTPUT total
← 12.0
RETURN:Sends a value back to wherever the subroutine was called. Execution stops at RETURN.
Multiple Parameters

Subroutines with Multiple Inputs

SUBROUTINE area(length, width)
  RETURN length * width
ENDSUBROUTINE

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