Learn
8.1.6B.1 | DEFINE AND CALL PROCEDURES
01 | MAKE A REPEATED TASK REUSABLE
A quiz may display the same instructions before several rounds. Copying that block everywhere works at first, but a later change must be made in every copy. A named procedure lets the program reuse one definition.
A procedure is a named routine that performs a task without returning a value to replace its call in an expression. A routine is a group of instructions given a name. Procedures can display information, repeat actions and make decisions.
In this lesson you will write the definition, call it from the main program and trace what happens. The main program is the part coordinating the overall task.
02 | DEFINE A PROCEDURE WITHOUT PARAMETERS
A definition specifies the procedure’s name and instructions. In Cambridge pseudocode, begin with PROCEDURE and the identifier, then finish with ENDPROCEDURE.
The body is the group of instructions between those keywords. ShowInstructions has two OUTPUT instructions in its body. They describe one useful task together.
This procedure needs no supplied information, so it has no parameters. Cambridge’s no-parameter notation uses the name on its own, as shown here. Definitions belong at the start of the code.
PROCEDURE ShowInstructions OUTPUT "Answer each question" OUTPUT "You receive five points for a correct answer" ENDPROCEDURE
03 | CALL THE PROCEDURE TO EXECUTE ITS BODY
A definition does not run its body merely because it appears in the code. A call asks the procedure to execute. Use CALL followed by the name.
The caller is the part of the program making the call. When execution reaches CALL ShowInstructions, it moves into that procedure, runs its body and then continues at the instruction after the call.
The output order is “Round one”, the two instruction messages, then “Begin”. ENDPROCEDURE ends the procedure’s execution; it does not end the whole program.
PROCEDURE ShowInstructions OUTPUT "Answer each question" OUTPUT "You receive five points for a correct answer" ENDPROCEDURE OUTPUT "Round one" CALL ShowInstructions OUTPUT "Begin"
04 | CALL THE SAME DEFINITION MORE THAN ONCE
Each call executes the body again. The definition below appears once, but ShowReady is called twice, so its message appears twice.
The caller continues after the first call, outputs “Round two”, then reaches the second call. A procedure is not a loop that repeats by itself: the two explicit calls are what make this task run twice.
PROCEDURE ShowReady OUTPUT "Ready to start" ENDPROCEDURE OUTPUT "Round one" CALL ShowReady OUTPUT "Round two" CALL ShowReady
| Caller action | Output |
|---|---|
| First OUTPUT | Round one |
| First CALL | Ready to start |
| Second OUTPUT | Round two |
| Second CALL | Ready to start |
05 | DEFINE A PARAMETER FOR CHANGING INFORMATION
A parameter is a named item in the procedure definition that receives a value supplied by the caller. It makes the task flexible without rewriting its body.
PlayerName is declared as a STRING parameter in the header. Inside Greet, the OUTPUT instruction uses that parameter’s current value.
The literal text “Hello, ” stays the same on every call. The value referred to by PlayerName changes according to what the caller supplies. Do not put PlayerName in quotes when you intend to output its value.
PROCEDURE Greet(PlayerName : STRING) OUTPUT "Hello, ", PlayerName ENDPROCEDURE
06 | SUPPLY AN ARGUMENT IN THE CALL
An argument is the value or expression supplied at a call, also described as a parameter value. Write it in parentheses after the procedure’s name.
At the first call, PlayerName receives “Maya”; at the second, it receives “Leo”. Each execution uses the same body but a different value. The outputs are “Hello, Maya” and “Hello, Leo”.
The parameter name belongs to the definition; the argument belongs to the call. A call supplies information to a procedure, whereas INPUT receives information from outside the program.
CALL Greet("Maya")
CALL Greet("Leo")07 | A CALLER VARIABLE CAN SUPPLY THE VALUE
The caller need not supply a literal. It can supply the current value of a variable. The caller’s variable and the procedure’s parameter do not need the same name.
StudentName holds “Aisha”, so Greet receives that STRING value as PlayerName. Changing StudentName before a later call changes the value supplied by that later call.
Do not assume passing a parameter automatically changes the caller’s variable. These examples only read the supplied values and output messages; they make no claim about changing argument variables inside a procedure.
DECLARE StudentName : STRING StudentName ← "Aisha" CALL Greet(StudentName) StudentName ← "Noah" CALL Greet(StudentName)
08 | MATCH PARAMETER NUMBER, ORDER AND TYPE
ShowScore needs a name and a score. Its definition lists the STRING name first and the INTEGER points second. Each call must supply the corresponding values in that order.
CALL ShowScore("Maya", 12) maps “Maya” to PlayerName and twelve to Points. The same positional rule applies when both parameters have the same type, so meaningful names and careful reading still matter.
Supplying too few values, too many values or unsuitable types does not match this definition. The syllabus permits procedures with up to three parameters.
PROCEDURE ShowScore(PlayerName : STRING, Points : INTEGER)
OUTPUT PlayerName, " scored ", Points
ENDPROCEDURE
CALL ShowScore("Maya", 12)| Parameter position | Expected parameter | Supplied argument |
|---|---|---|
| 1 | PlayerName : STRING | "Maya" |
| 2 | Points : INTEGER | 12 |
09 | THE BODY CAN CONTAIN A LOOP
A procedure body is not limited to a single instruction. RepeatMessage uses a loop to output Message the number of times specified by Times.
Counter is declared inside the procedure and controls the FOR loop. It is different from Times, which supplies the requested number of repetitions. The next scope lesson explores variables declared inside routines in more detail.
Assume Times is a non-negative INTEGER. With Times equal to three, the body outputs “Keep going” three times. With Times equal to zero, this ascending FOR executes no iterations, so there is no message output.
PROCEDURE RepeatMessage(Message : STRING, Times : INTEGER)
DECLARE Counter : INTEGER
FOR Counter ← 1 TO Times
OUTPUT Message
NEXT Counter
ENDPROCEDURE
CALL RepeatMessage("Keep going", 3)10 | STEP THROUGH A PARAMETERISED CALL
Choose a message and repetition count, then follow the call into RepeatMessage. The trace shows the supplied values, each OUTPUT execution and the continuation after the procedure finishes.
Try zero repetitions. The call still happens, but its FOR body executes zero times. A procedure’s execution and its inner loop’s iterations are different counts.
11 | PUT SELECTION INSIDE A PROCEDURE
A procedure can also make a decision. ShowGrade receives an INTEGER score and displays one category. Assume the score is already valid and lies from zero to one hundred.
The call supplies a score, then the IF statements in the body choose a message. The procedure outputs the category; it does not return that category as a function result.
CALL ShowGrade(80) displays “Distinction”; CALL ShowGrade(50) displays “Pass”; CALL ShowGrade(49) displays “Not a pass”. The caller resumes after each call.
PROCEDURE ShowGrade(Score : INTEGER)
IF Score >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Not a pass"
ENDIF
ENDIF
ENDPROCEDURE12 | THREE PARAMETERS CAN DESCRIBE ONE TASK
A display task might need a name, points earned and maximum points. Three parameters are enough to provide all that information explicitly.
For the call below, the output is “Maya: 12 out of 20”. Points and Maximum are both INTEGER, so reversing twelve and twenty would still supply the expected types but communicate the wrong meaning. Types alone do not prove the call is correct.
This example assumes the supplied values are suitable for the report. The parameter list does not automatically check that Points is no greater than Maximum.
PROCEDURE ShowResult(PlayerName : STRING, Points : INTEGER, Maximum : INTEGER)
OUTPUT PlayerName, ": ", Points, " out of ", Maximum
ENDPROCEDURE
CALL ShowResult("Maya", 12, 20)13 | ONE PROCEDURE CAN CALL ANOTHER
Procedures can cooperate. ShowRound calls Greet, then outputs a ready message. When Greet finishes, execution returns to the next instruction inside ShowRound, not directly to the main program.
After ShowRound finishes, execution returns to the main program after CALL ShowRound. The output order is “Hello, Maya”, “Round ready”, then “Main program continues”.
Follow the immediate caller at each return. This is a nested call, not an instruction to run both routines at the same time.
PROCEDURE Greet(PlayerName : STRING)
OUTPUT "Hello, ", PlayerName
ENDPROCEDURE
PROCEDURE ShowRound(PlayerName : STRING)
CALL Greet(PlayerName)
OUTPUT "Round ready"
ENDPROCEDURE
CALL ShowRound("Maya")
OUTPUT "Main program continues"14 | FIX COMMON DEFINITION AND CALL ERRORS
Definition without a call: the task has been described but the main program never asks it to run.
Quotes around a parameter name: OUTPUT "PlayerName" prints that word; OUTPUT PlayerName prints the supplied value.
Arguments in the wrong order: check their roles as well as their types, especially when several parameters use the same type.
Missing ENDPROCEDURE: close the definition so its body and the following code are clearly separated.
Expecting a returned result: do not write Grade ← ShowGrade(80) for this output procedure. Use CALL ShowGrade(80). A function is needed when the caller requires a result to use in an expression.
15 | PLAN, CALL AND TEST
Choose a meaningful name describing one clear task. Decide what information it needs from its caller, then write parameters with suitable types. Define the body before writing calls in the main program.
Test each call by recording the supplied values, their corresponding parameter names, the instructions executed and the next instruction in the caller. Include boundary cases for any decisions or loops in the body.
A useful explanation is: “The call supplies three as Times, therefore the FOR body executes three times. After ENDPROCEDURE, the caller continues after the call.” This connects the supplied value to the procedure’s behaviour.
The next lesson develops functions, where a returned value replaces the call in an expression rather than CALL acting as a complete statement.