Functions: reusable named actions — Computing, 11–13 years
A function gives a useful group of instructions a name, so it can be used without rebuilding it each time.
A named action
A function is a small, named job inside a program. You define what the job does once, then call its name whenever you need it; some functions also receive inputs and produce an answer.
Why package instructions
Without functions, the same instructions would be copied wherever they are needed. That makes programs long and fragile: one correction must be repeated everywhere. Functions came from the need to organise large programs and give each useful job one place to maintain.
A function for price
Make a function called total that receives a price and adds 2 euros for delivery. Call total with 5: 5 plus 2 gives 7. Call it with 12: the same named job gives 14, so the rule is written once but used twice.
The hidden input
A common mistake is making a function depend on a particular outside value, then expecting it to work everywhere. This feels convenient at first, but it hides what the job needs. Give important inputs clearly, so the function can be tested and reused.
Reusable tools
Functions appear in games for jumping, in maps for calculating distance, and in apps for checking a password. Outside programming, a recipe or a reusable machine setting works similarly: one reliable method can serve many cases with new inputs.
Keep exploring
Other languages
Loading MyLeoNes™…