CIE IGCSE | 8.1.6B.1 Define and Call Procedures

Lesson objective

Define and call procedures with and without parameters, match arguments to parameter types and positions, and trace execution back to the caller.

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
One definition, two separate executions
Caller actionOutput
First OUTPUTRound one
First CALLReady to start
Second OUTPUTRound two
Second CALLReady 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)
Argument values are matched by position
Parameter positionExpected parameterSupplied argument
1PlayerName : STRING"Maya"
2Points : INTEGER12

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
    ENDPROCEDURE

    12 | 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.

    MATCH THE PROCEDURE CODE TO ITS ROLE

    Terminology

    Terminology

    Procedure

    A named routine performing a task without returning a function result to its calling expression.

    Definition

    The code specifying the procedure’s name, parameters if needed, and body.

    PROCEDURE

    The keyword beginning a procedure definition.

    ENDPROCEDURE

    The keyword ending a procedure definition.

    Body

    The instructions inside a procedure that run when called.

    Call

    An instruction asking a routine to execute.

    CALL

    The Cambridge keyword used to invoke a procedure as a complete statement.

    Caller

    The part of the program that makes a call.

    Parameter

    A named item in the definition receiving a supplied value.

    Argument

    A value or expression supplied at a call, also described as a parameter value.

    Parameter type

    The kind of value a parameter expects.

    Positional matching

    Matching supplied arguments to parameters in the definition’s order.

    Literal argument

    A directly written value supplied in a call.

    Variable argument

    A caller variable whose current value is supplied at a call.

    Main program

    The instructions coordinating the overall program, including routine calls.

    Trace

    A step-by-step record of execution and relevant values.

    Reuse

    Calling one definition for the same task in different places or with different values.

    Nested call

    A call made from within another routine, returning to that immediate caller first.

    Questions

    Questions

    CHECK YOUR UNDERSTANDING

    Select all correct choices. Each exact set earns one point.

    1. Which statements describe a procedure definition?
    2. Which is the Cambridge call for a no-parameter procedure ShowInstructions?
    3. Greet(PlayerName : STRING) is called with "Maya". Which are correct?
    4. ShowScore expects STRING then INTEGER. Which calls match?
    5. RepeatMessage is called with Times 0. Which are correct?
    6. ShowResult expects name, points and maximum. What does reversing points and maximum do?
    7. ShowRound calls Greet before outputting Round ready. What happens after Greet finishes?
    8. Which are suitable checks for procedure code?

    EXPLAIN AND COMPARE

    Write your answers before revealing each sample.

    1. Define a procedure and describe its body.

    2. Describe execution of OUTPUT "Before", CALL ShowReady, OUTPUT "After" when ShowReady outputs Ready.

    3. Explain the parameter and argument in Greet(PlayerName : STRING) and CALL Greet("Maya").

    4. Explain why CALL Greet(StudentName) does not require the parameter to be named StudentName.

    5. Trace CALL RepeatMessage("Go", 3) and compare it with Times zero.

    6. Explain why reversing two INTEGER arguments can still be wrong.

    7. Describe the return route when ShowRound calls Greet.

    8. Explain why Grade ← ShowGrade(80) is unsuitable for the procedure in this lesson.

    BUILD IT | A PERSONALISED REMINDER

    Define Remind with STRING parameter StudentName and INTEGER parameter Times. Output “Remember to save, ” followed by the supplied name Times times. Assume Times is non-negative. Call it for Maya with two reminders and Leo with zero, then output “Reminders complete” in the main program. Declare a separate loop counter inside the procedure. Predict all outputs.

    Reveal one possible solution
    PROCEDURE Remind(StudentName : STRING, Times : INTEGER)
      DECLARE Counter : INTEGER
      FOR Counter ← 1 TO Times
        OUTPUT "Remember to save, ", StudentName
      NEXT Counter
    ENDPROCEDURE
    
    CALL Remind("Maya", 2)
    CALL Remind("Leo", 0)
    OUTPUT "Reminders complete"

    The output is “Remember to save, Maya” twice, then “Reminders complete”. The Leo call occurs but its inner FOR has no iterations, so it outputs no reminder. Each call uses one reusable definition with its own supplied parameter values.

    Flashcards

    Flashcards

    Click to flip. Select the ideas you need to revisit.

    0 cards selected for revision.

      Selections are kept while this page is open.

      Workbook

      Workbook

      COMING SOON

      The workbook for 8.1.6B.1 Define and Call Procedures is coming soon. Complete the matching task and written questions in your exercise book.