Computer ScienceExam code: 0984

Procedures and Functions

Programming

A question may test this difference directly, for example by asking which kind of sub-program always returns a value.

FeatureProcedureFunction
Returns a value?NoYes
Keyword used to definePROCEDURE ... ENDPROCEDUREFUNCTION ... ENDFUNCTION
Uses RETURNS in header?NoYes (RETURNS <data type>)
Contains RETURN <value>?NoYes (the value to send back)
How it is calledWith CALL keywordUsed inside an expression (no CALL)
Typical useDoing something (output, save, update state)Calculating something and giving the value back
Example pseudocode callCALL CalculateArea(5, 3)Result ← CalculateArea(5, 3)

Quick rule of thumb

  • If the sub-program's job is to DO something, write a procedure and call it with CALL.
  • If the sub-program's job is to PRODUCE a value, write a function and use it inside an expression.

Exam tip

Describing what happens when a function is called

Asked what happens when a function is called (3 marks), give four steps in order: the function is called by its identifier; any parameters are passed to it from the main program; it carries out its task; and it returns a value to the main program. Any three earn the marks, but the returned value is what shows you are describing a function rather than a procedure, so always include it. Describe the call in words, not with CALL, which is for procedures only: a function sits inside an expression, and its returned value takes the place of the call.

Quick check

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

About 1 minute

Build on this topic