Procedures and Functions
Programming
A question may test this difference directly, for example by asking which kind of sub-program always returns a value.
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No | Yes |
| Keyword used to define | PROCEDURE ... ENDPROCEDURE | FUNCTION ... ENDFUNCTION |
Uses RETURNS in header? | No | Yes (RETURNS <data type>) |
Contains RETURN <value>? | No | Yes (the value to send back) |
| How it is called | With CALL keyword | Used inside an expression (no CALL) |
| Typical use | Doing something (output, save, update state) | Calculating something and giving the value back |
| Example pseudocode call | CALL 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.