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.
PROCEDURE Greet()
OUTPUT "Ready"
END PROCEDURE
CALL Greet()PROCEDURE Greet()
OUTPUT "Ready"
ENDPROCEDURE
CALL Greet()Ready
A parameter is declared in the brackets, with a type. The caller passes the value. The procedure does not INPUT it again.
PROCEDURE Square(N : INTEGER)
OUTPUT N * N
ENDPROCEDURE
CALL Square(6)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.
FUNCTION Double(N : INTEGER) RETURNS INTEGER
RETURN N * 2
ENDFUNCTION
OUTPUT Double(5)10
| The question says | You 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 assignment | A function: OUTPUT Double(5) |
| It only needs to print or store, and nothing comes back | A 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
- How to fill a trace table the way the mark scheme doesFor students
- Cambridge arrays start at 1For students
- The validation checks Paper 2 actually asks forFor students