Computer ScienceExam code: 0984

Procedures and Functions

Programming

A procedure is a named block of code that performs a task without returning a value. It is invoked using the CALL keyword.

Defining a procedure

A procedure with no parameters:

PROCEDURE ProcedureName
   // statements that do the work
ENDPROCEDURE

A procedure with (values passed in):

PROCEDURE ProcedureName(Param1 : TYPE, Param2 : TYPE, ...)
   // statements that use the parameters
ENDPROCEDURE

Each parameter is named and given a data type, so the procedure knows what kind of value to expect.

Calling a procedure

Procedures are run using the CALL keyword:

CALL ProcedureName
CALL ProcedureName(Value1, Value2)

The values in brackets are called . Each argument is matched in order to the corresponding parameter in the procedure's definition.

Exam tip

Writing the line that calls a procedure

Asked for the pseudocode that uses a named procedure and passes it values, write one line: CALL, the procedure's name, then the values in brackets, separated by commas and in the order of its parameters, as in CALL ShowTotal(12, 40). One scheme gives a mark for the keyword CALL and a second for the name with the right values, so leaving out CALL costs a mark even when the rest is right. You need not write the procedure itself. A function is never called this way: it goes inside an expression, as in Total ← AddUp(12, 40).

When to use a procedure

Choose a procedure when the sub-program's job is to do something rather than to compute and return something:

  • Show a menu on screen.
  • Save data to a file.
  • Display a report.
  • Reset all the variables to a known starting state.

Quick check

4 short questions on this note. See what has stuck, and where to look if something hasn't.

About 2 minutes

Build on this topic