Functions & Procedures

💻 Computer Science Years 8–11 Foundation

Named, reusable subprograms: the difference between functions and procedures, and how parameters, arguments and return values work.

Open this lesson, with practice and a printable worksheet →

Imagine writing out the full recipe for tea every single time you wanted a cup. Programmers refuse to do that. Instead they package a useful block of code, give it a name, and call that name whenever they need it. These named blocks are subprograms — and they are what turn a tangle of code into something you can actually read.

Why this matters: Real programs are thousands of lines long. Functions and procedures let you write a task once and reuse it everywhere, which means less code, fewer bugs, and changes made in one place instead of fifty. Decomposition — breaking a big problem into named sub-tasks — is one of the core ideas the exam rewards.
area() w × h input w, h return value parameters go in → a value comes back
A function is a machine: feed it parameters, it returns a result.

Function or procedure?

FUNCTIONarea()15gives a valuePROCEDUREshow()—no value back
A function hands a value back; a procedure just acts.

Both are named, reusable blocks. The difference is the payback. A function hands a value back to whatever called it, using return. A procedure just does a job — it prints something or updates the screen — and returns nothing. Rule of thumb: if you want an answer, write a function; if you just want an action, write a procedure.

Parameters: the ingredients you pass in

the call (arguments)area(5, 3)the definition (parameters)area(w, h)5 → w3 → h
Arguments in the call fill the parameters in the definition.

A subprogram often needs information to work. Those inputs are parameters, listed in brackets after the name. When you call the subprogram and supply real values, those values are called arguments. So area(5, 3) passes the arguments 5 and 3 into the parameters w and h.

An analogy: a vending machine

A vending machine is a function. You put in a choice (the parameter), a process happens inside, and a can drops out (the return value). You do not need to know the wiring — you just use it. That hiding of detail is called abstraction, and it is exactly why subprograms make big programs manageable.

TermMeaning
FunctionA subprogram that returns a value
ProcedureA subprogram that performs an action, returns nothing
ParameterA named input in the subprogram's definition
ArgumentThe actual value passed in when it is called
ReturnSends a value back to the caller
Worked example — a reusable function.
area(w,h)written oncearea(5,3)area(10,2)1520
One definition, called twice — each call returns its own value.
function area(w, h)
    result = w * h
    return result
endfunction

rug = area(5, 3)      # rug is now 15
wall = area(10, 2)    # wall is now 20
Written once, called twice with different arguments. Each call returns its own value.
Common mistake: forgetting the return line and expecting an answer anyway. Without return, a function calculates inside itself but hands nothing back — the caller receives nothing (often None or null).

Tip: name a subprogram after the job it does — calculateTotal, not doStuff. A good name means you can read the program almost like plain English.

Get the printable worksheet →
← Strings → Files & Data
← Sasta Basta home All subjects