Paper 2 · Routines

PROCEDURE or FUNCTION: which one the question wants

· 6 min read · For students

Paper 2 uses the word in the question, and the two shapes are not interchangeable. A PROCEDURE performs an action. A FUNCTION calculates a value and gives it back. If you CALL a function, or try to OUTPUT a procedure, the structure mark goes.

A procedure is an action

It has a name, optional parameters with types, a body, and ENDPROCEDURE. The main program runs it with CALL. It does not sit inside an expression.

WrongThe closer is one word. END PROCEDURE is two.
PROCEDURE Greet()
    OUTPUT "Ready"
END PROCEDURE

CALL Greet()
Right
PROCEDURE Greet()
    OUTPUT "Ready"
ENDPROCEDURE

CALL Greet()
Output
Ready

A parameter is declared in the brackets, with a type. The caller passes the value. The procedure does not INPUT it again.

Right
PROCEDURE Square(N : INTEGER)
    OUTPUT N * N
ENDPROCEDURE

CALL Square(6)
Output
36

A function returns a value

The header says what it returns: FUNCTION Double(N : INTEGER) RETURNS INTEGER. The body RETURNs one value. You use the name in an expression. There is no CALL.

Right
FUNCTION Double(N : INTEGER) RETURNS INTEGER
    RETURN N * 2
ENDFUNCTION

OUTPUT Double(5)
Output
10
The question saysYou write
Write a procedure …PROCEDURE … ENDPROCEDURE, then CALL Name(...)
Write a function …FUNCTION … RETURNS type … RETURN … ENDFUNCTION
Use the result in an OUTPUT or an assignmentA function: OUTPUT Double(5)
It only needs to print or store, and nothing comes backA procedure

The PROCEDURE lesson and the FUNCTION lesson are the short versions. The reference pages are procedures and functions.

Check your own code

Run it in the compiler. A mistake is explained in plain English, and many of them have a one-click fix.

More from the blog