One-line answerQuestion 1 · Recall
Loading…
By the end you can write a procedure in 9618 pseudocode, call it with CALL, and say
exactly where in an algorithm a procedure belongs.
A shop program prints a receipt after a sale, after a refund, and after an exchange.
You could copy the same six OUTPUT statements into all three places. It works. Then the
shop adds a VAT line.
Now you have three copies to find and edit, and three chances to miss one. The program still runs — it just prints the wrong receipt in two places, which is worse than a crash, because nobody notices.
A procedure removes the copies. You write the statements once, give them a name, and run them by calling that name.
Procedure
A procedure is a named block of statements that performs a task. It is defined once,
between PROCEDURE and ENDPROCEDURE, and carried out by a CALL statement.
A procedure does not return a value.
That last sentence is the whole difference between a procedure and a function, and it is the difference examiners test most often.
PROCEDURE PrintDivider()
OUTPUT "------------------------------"
ENDPROCEDURE
PROCEDURE PrintDivider(). It names the procedure. Empty brackets
mean it takes nothing in; they are still written.ENDPROCEDURE. These statements run
when the procedure is called.ENDPROCEDURE — closes the definition. Nothing after it is part of the procedure.Notice that nothing has printed yet. A definition is a set of instructions in a drawer, waiting to be used — like a recipe card rather than the cooking.
To use it, write CALL:
OUTPUT "Receipt"
CALL PrintDivider()
OUTPUT "Total: ", Total
CALL PrintDivider()
CALL PrintDivider() means "run the statements with that name, then come back to the line
after this one". The output is
Receipt
------------------------------
Total: 12.50
------------------------------
Two calls, one definition, three lines of output.
Worked example: Worked example — a receipt banner procedure
A program prints this banner after every sale, refund and exchange:
==============================
Thank you for shopping with us
==============================
Write a procedure that prints it, and show it being called.