Imagine you are writing a program for a school prize-giving. Every single pupil needs a cheery
welcome message, a line of decorative stars, and their name in capitals. There are
There is a much better way. You write the welcome routine once, give it a name, and then just say that name whenever you want it to happen. That named, reusable block of code is called a subroutine, and using it by name is called calling it.
Nearly every language has them; you have already been using one every time you write
console.log(...) — that is a subroutine someone else wrote for you.
greet
Here is the whole idea in miniature. We define a subroutine called greet,
and then call it three times. Notice how the greeting is written out only once, but
happens three times. Press Run:
The word function begins the definition, then comes the name greet, then
the body in curly braces { } — the actual work. Writing that down does
not run it. The program only springs to life at the three greet() calls near
the bottom. Each call jumps up to the body, runs every line, then comes back to carry on where it
left off.
Subroutines come in two kinds, and the difference is simple but important:
greet above is a procedure.Think of the difference like asking a helper to do a job. A procedure is "please tidy the classroom" — they do it, and that's that. A function is "please count the chairs" — they do the work and come back and tell you the number, which you can then write down or use.
Very nearly, and that's where the name comes from. In maths,
square
Here is a tiny function. It works out a number squared and hands the answer back using the keyword
return. Because it returns something, we can catch that value — store it in a
variable, or drop it straight into a console.log:
See how square(5) behaves like the number greet
can't be used like this, because it gives nothing back.
areaOfRectangle
Subroutines aren't only about saving typing — they let you break a big problem into named
pieces. A good name is like a label on a box: you can use "work out the area of a rectangle"
without re-reading how it's done every time. Here areaOfRectangle is a function
that returns an area, and we reuse it for several rooms:
The reader of your program sees areaOfRectangle(4, 3) and instantly understands the
intent — far clearer than a bare 4 * 3 buried in the middle of a long calculation. And
if the formula ever needed changing, there is one place to change it.
(Here width and height are parameters — the inputs a
subroutine is given — and there is a whole page about passing inputs and getting outputs:
Once you have subroutines, three good things follow — worth learning by name for your exam:
Look back at greet: if the head teacher wants a different message next year, you edit
the body once and all three calls (or all
Two classic beginner slip-ups, both about the difference between defining a subroutine and calling it.
1. Defining it is not the same as running it. Writing a function
block only teaches the computer what to do — it doesn't do it. Nothing happens until you
call the subroutine by name with its brackets. This program prints
nothing at all, because greet is defined but never called:
2. A function's returned value must be used, or it's lost. When a function
returns a value, that value floats back to the call — and if you don't
catch it (store it in a variable or print it), it vanishes. This line quietly throws the
answer away:
The habit to build: after a function call, ask yourself "where does the returned value go?" If the answer is "nowhere", you've probably made a mistake.