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.
Function or procedure?
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
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.
| Term | Meaning |
|---|---|
| Function | A subprogram that returns a value |
| Procedure | A subprogram that performs an action, returns nothing |
| Parameter | A named input in the subprogram's definition |
| Argument | The actual value passed in when it is called |
| Return | Sends a value back to the caller |
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.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.