Functions and abstraction — Computing, 14–17 years
A function gives a useful name to a task, hides its internal steps and allows the task to be reused. This helps people build larger programs without holding every detail in mind.
A named mini-program
A function is a named piece of code that performs one task. It can receive inputs, use them in its steps and send back an output. The rest of the program can use the function by its name without needing to know how every step works inside.
Why abstraction matters
Large programs became difficult when every part had to know every low-level detail of every other part. Functions solve this problem by setting a small, clear boundary: what goes in and what comes out. Programmers can improve the hidden steps without changing every place that uses the function.
A function for tax
Define final_price(price, rate) as price + price × rate. Call final_price(50, 0.2): first calculate 50 × 0.2 = 10, then add 50 to get 60. Call it again with 80 and 0.2: 80 × 0.2 = 16, so the result is 96. The same named task handles both prices.
The boundary is a promise
A common mistake is to assume a function magically knows the right kind of input. It does not: its inputs must match the promise made by its design, such as a number rather than text. This mistake is reasonable because the function name hides the inner steps, so the input rules must be made clear.
Abstraction in everyday software
When an app asks for your location, a function may request the phone’s sensors and turn their readings into coordinates. Other parts only use a name such as get_location(). They do not need to know the sensor protocol, just as a driver uses a brake pedal without designing the braking system.
Keep exploring
Other languages
Loading MyLeoNes™…