Portmint Lighthouse

Functions, Bundling Steps

Here is the plain idea: a function is a bundle of steps wrapped up under a single name. Once you've defined it, you can run all those steps anytime, anywhere, just by calling the name — without writing the steps out again.

Define it once, use it as often as you like. That's the whole point.

The kitchen-appliance analogy

Think of a blender. Inside, it does a dozen little things — spinning blades, mixing, chopping. But you don't think about any of that. You drop in fruit, press one button labeled "blend," and out comes a smoothie.

A function is that button. Someone wrote the messy steps once and tucked them behind a single name. From then on, you press the button — call the function — and the steps run. You don't re-explain how to blend every time you want a smoothie.

Inputs and outputs

A good appliance takes something in and gives something back. A blender takes fruit and returns a smoothie. A function works the same way.

The thing you hand in is an input (programmers say argument). The thing you get back is the output (the return value). A function called add(3, 5) takes two numbers in and hands 8 back. A function called greet("Cole") takes a name and hands back "Hello, Cole."

Same button, different fruit, different smoothie. You can call one function with many different inputs and get a fitting result each time. That's what makes functions so reusable.

Why bundle at all?

Three plain wins.

Less repetition. Write the steps once, behind a name, instead of copying them everywhere. If twenty places need to calculate tax, they all call calculate_tax — they don't each spell it out.

Easier fixes. When the steps live in one place, you fix a bug in one place. Improve the blender, and every smoothie improves.

Clearer reading. A program built from well-named functions reads like a summary: load_orders, calculate_total, send_receipt. You can follow the story without drowning in every detail. The details wait inside, for when you actually need them.

They build on everything so far

Functions don't replace what you've learned — they hold it. Inside a function you'll find variables holding values, conditionals making decisions, loops repeating work. The function is the wrapper that gives that whole little routine a name and a handle.

This is the big move in programming: take something complicated, get it working once, wrap it in a name, and from then on treat it as one simple thing. Whole programs are built by stacking these bundles, each one trusting the ones below it.

Your turn

No computer needed — just answer on paper. Imagine a function called area that takes two inputs, width and height, and returns width times height.

  1. What would area(3, 4) hand back? (Multiply them.)
  2. What would area(10, 2) hand back?
  3. Now invent your own. Name a function for an everyday task, say what goes in, and say what comes back — for example, "tip, takes a bill amount, returns 20 percent of it."

(Same button, different fruit: area(3, 4) hands back 12, and area(10, 2) hands back 20.) If you can name the button, name what you feed it, and name what it returns, you understand functions.

So: a function is a labeled button, you press it by calling its name, you can feed it inputs and catch its output, and it lets you stop repeating yourself. Next, we'll learn to hold many values at once. 🔦

Stuck or curious?

Ask Pip about this lesson — tap the porthole bottom-right.