What’s the Difference Between a Procedure and a Function?
Learn the difference between a procedure and a function in GCSE Computer Science, with a simple example showing why output is not the same as returning a value.
A function returns a value to the part of the program that called it. A procedure performs a task without returning a value. Both are named sections of code that you can run when needed; these are called subprograms.
What does “return” mean?
Returning a value sends a result back to the caller, so the program can store it, use it in a calculation or pass it to another subprogram. A procedure can still produce a visible result, such as displaying text. Displaying something on screen is output, though: it does not send a value back to the caller.
Worked example
PROCEDURE showGreeting()
OUTPUT "Hello"
ENDPROCEDURE
FUNCTION makeGreeting()
RETURN "Hello"
ENDFUNCTION
CALL showGreeting()
SET message TO makeGreeting()
Here, showGreeting() displays Hello but gives the calling code no value to store. makeGreeting() returns the text Hello, which the calling code stores in message. You could then display message later or use it elsewhere. The exact syntax for defining and calling subprograms varies between programming languages, but the distinction between outputting and returning is the important part of this example.
Both types of subprogram can take parameters: values supplied when they are called. For example, either greeting subprogram could take a name as a parameter and use it to create a personalised greeting. Having parameters does not make a subprogram a function; what matters is whether it returns a value.
A useful way to decide which to write is to ask what the calling code needs next. If it needs a result for a later step, use a function. If it only needs an action carried out, such as displaying a greeting, use a procedure.
Common exam mistake: saying a procedure “returns Hello” because it prints Hello. It outputs text, but it does not return a value for the caller to use.