Fill in the blanksQuestion 1 · Recall
Loading…
By the end you can write a subprogram that takes parameters, pass a value by value and by
reference (using BYREF), predict what happens to the original variable in each case, and
use the terms header, interface, parameter, argument and return value correctly.
The banner procedure from the procedures lesson prints the same text every time it is called. That is fine for a banner, and useless for a receipt.
A receipt needs the customer's name and the amount — different on every sale. Write the procedure without them and it cannot print any particular sale. Write six versions, one per customer, and you are back to copy-and-paste.
Parameters solve this. They are the slots in the header where information comes in, so one procedure can work on different data on every call.
Parameter and argument
A parameter is a variable named in the header of a subprogram, which receives a value when the subprogram is called.
An argument is the value (or variable) actually supplied at the call.
Same idea, two sides of the call: the parameter is the slot, the argument is what you put in it.
PROCEDURE PrintTotal(CustomerName : STRING, Amount : REAL)
OUTPUT CustomerName, " owes ", Amount
ENDPROCEDURE
The header names two parameters, with a type for each:
CustomerName : STRING — the first slot, holding text;Amount : REAL — the second slot, holding a decimal number.To use it, supply two arguments in the same order, separated by commas:
CALL PrintTotal("Aisha", 24.50)
Now trace the call. CustomerName becomes "Aisha" and Amount becomes 24.50, so the
body prints Aisha owes 24.50.
A second call with different arguments prints something different:
CALL PrintTotal("Ben", 9.99) // Ben owes 9.99
Order matters, because the first argument always fills the first parameter. Swapping
them here would try to store "Aisha" in a REAL, which is a type error.
Subprograms may take no parameters (the empty brackets you have already seen), one, or several — the syllabus allows all three, and you choose based on what the task needs.
Objective 11.3.6 is about using these terms precisely, so here they are on one real subprogram:
FUNCTION LineTotal(UnitPrice : REAL, Quantity : INTEGER) RETURNS REAL
RETURN UnitPrice * Quantity
ENDFUNCTION
Cost ← LineTotal(2.50, 4)
| Term | What it means | In this example |
|---|---|---|
| Header | The first line of the definition: name, parameters, and (for a function) the return type | FUNCTION LineTotal(UnitPrice : REAL, Quantity : INTEGER) RETURNS REAL |
| Interface | The header — the information another programmer needs in order to call it, without reading the body | same line: give it a REAL and an INTEGER, get a REAL back |
| Parameter | A named slot in the header | UnitPrice, Quantity |
| Argument | A value supplied in the call | 2.50, 4 |
| Return value | The value a function hands back | 10.00 |
Two traps: