Procedures & Parameter Passing

A procedure is a named group of statements called to perform a task. Parameters describe the values it needs; arguments are the actual values supplied by a call. A procedure does not return a value to be used in an expression.

PROCEDURE ShowTotal(Price : INTEGER, Quantity : INTEGER)
    DECLARE Total : INTEGER
    Total <- Price * Quantity
    OUTPUT Total
ENDPROCEDURE
CALL ShowTotal(6, 4)

Worked call and scope

The arguments 6 and 4 become Price and Quantity within this call. Total becomes 24 and is output. Total is local to the procedure: another procedure cannot assume it can access that local variable. A global variable is declared outside subroutines and can be accessible throughout the program; use parameters where practical to make dependencies clearer.

Use CALL for a procedure. A function instead returns a value and can appear in an expression. Reference-passing mechanisms are optional extension, not needed to understand these examples.

Maintainability practice

A program prints the same three-line heading in five places. Explain why a named procedure makes a later heading change easier and reduces inconsistent copies. Do not introduce a procedure merely to hide one unexplained operation.

Three parameters and scope

PROCEDURE PrintTotal(A : INTEGER, B : INTEGER, C : INTEGER)
    DECLARE Total : INTEGER
    Total <- A + B + C
    OUTPUT Total
ENDPROCEDURE
CALL PrintTotal(2, 4, 7)

The parameters receive three argument values and the local Total is 13 during this call. A variable declared inside the procedure is local to that procedure. On paper change the procedure to output the average using ROUND, then call it with different arguments. Check that it uses all three parameters.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Application: Scope and calls

+30 XP

Application: Scope and calls

  1. Value supplied by a call: parameter or argument? [Space 1]
  2. Variable declared inside a procedure: local or global? [Space 2]
  3. Keyword used to invoke a procedure: [Space 3]
Word Bank / Available Cards (Drag into spaces or tap to place)
All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.