What Is a Function, and How Do You Dry Run It?

Author: codeplu.com
Last Updated: 29 Jul 2026
Est. Duration: 10 min
Skill Level: Beginner

Root Concept

A function is a box with a promise about what goes in and what comes out — and dry running means being the computer yourself, tracing each value by hand to catch mistakes before you run it.

CodePLU Goal

Upgrading Human Mental Models

Learn how to think in Workflows

Concept Playground
CodePLU logo

Concept Development By codeplu.com

From a one-sentence promise to a function you trust — with a by-hand trace before running

What Is a Function, in Plain Words?

Think about a kettle. You put water in, press the switch, and hot water comes out. You do not need to know anything about heating elements to use one, and you can rely on it because its promise is simple and always the same: water in, hot water out.

A function in programming is exactly that kind of box. It takes something in, does one job, and hands something back. Give it the same thing and you get the same thing out. That reliability is the entire point — once a function keeps its promise, you can use it anywhere without thinking about its insides again.

There is also a habit that belongs with functions and is rarely taught early enough: the dry run. Before running your code, you become the computer — take a real example value and follow your own steps by hand, writing down what each value is at each moment. It sounds slow. It is dramatically faster than hunting a bug afterwards, and in the playground above you will put it exactly where it belongs in the design process.

How Do You Design and Check a Function?

1

Why should a function do only one job?

Because a box with one promise is easy to trust, to reuse, and to fix. If you can describe your function in a single sentence without using the word 'and', it is probably the right size: 'work out the total price of a basket' is one job, while 'work out the total and email the receipt and update the stock' is three boxes pretending to be one. Beginners often grow one giant function because it feels efficient. The cost arrives later, when something breaks and there is nowhere to look but everywhere — and when you need that total somewhere else but cannot use it without also sending an email.

2

What does a function's promise consist of?

Two halves: what must be given to it, and what it hands back. Deciding both before writing any logic is what programmers mean by defining the contract. For a basket total you might promise: give me a list of items, each with a price and a quantity, and I will give you back a single number. Stating it that plainly forces the awkward questions into the open early — what should happen with an empty list, a quantity of zero, or a missing price? Answering those now is designing. Discovering them later, while the program is running, is debugging.

3

How do you write the steps inside?

Write the smallest logic that keeps the promise, and nothing more. For the basket that is: start a running total at zero, go through each item, multiply its price by its quantity, add that to the total, and hand back the total at the end. Notice how little there is once the promise is settled — most of the difficulty in programming lives in deciding what a piece of code should do, not in the doing. Resist adding features nobody asked for, because every extra concern inside the box is another way for it to break and another reason it cannot be reused.

4

What exactly is a dry run?

It is running the code in your head and on paper instead of on the computer. Pick a real, small example and walk through your steps one at a time, writing down every value as it changes. For a basket of two apples at 30 each and one loaf at 90: the total starts at 0, becomes 60 after the apples, becomes 150 after the loaf, and 150 is handed back. Then check that against the answer you already know. The reason this works so well is that it exposes the mistake at the exact step where your thinking went wrong, rather than showing you a wrong number at the end with no clue how it got there.

5

Why trace by hand when you could just run it?

Because running it tells you only whether the answer was wrong, while a dry run tells you where. Plenty of bugs produce a plausible-looking number, and those are the dangerous ones — nothing crashes, nothing warns you, and the total is quietly short by one item. Tracing also catches the classic beginner slips almost instantly: resetting the running total inside the loop instead of before it, or handing the answer back too early so only the first item counts. And the skill compounds, because reading code and knowing what it will do without running it is what makes someone genuinely fluent.

javascript
1// Promise: give me a list of items with price and quantity,2// and I hand back one number — the total.3function calculateTotal(items) {4  let total = 0;                 // start before the loop, not inside it5 6  for (const item of items) {7    total += item.price * item.quantity;8  }9 10  return total;                  // hand back after the loop, not during it11}
7

How do you dry run the function above?

Take the basket: two apples at 30 each, one loaf at 90. Make a small table with a column for the item and a column for the total. Before the loop, total is 0. First item, apples: 30 times 2 is 60, so total becomes 60. Second item, loaf: 90 times 1 is 90, so total becomes 150. The loop ends and 150 is handed back — which matches the answer you already knew. Now try the awkward case, an empty list: the loop body never runs, total stays 0, and 0 is handed back. Is 0 the right promise for an empty basket? Deciding that is design work, and the dry run is what surfaced the question.

Real World Example

Two tiny mistakes, both of which run happily and give the wrong answer.

How Does a Dry Run Catch a Bug Nothing Else Warns You About?

The same basket function, written with two of the most common beginner slips. Neither one crashes. A dry run finds both in under a minute.

1

The first slip: resetting inside the loop

Imagine the line that starts the total at zero is written inside the loop instead of before it. The program runs perfectly, produces a number, and no error appears anywhere. That number happens to be the last item's price only — which, for a real basket, looks entirely plausible.

2

The dry run exposes it immediately

Trace it: item one, total resets to 0, becomes 60. Item two, total resets to 0 again, becomes 90. Answer 90 instead of 150. Watching the total collapse back to zero at the start of the second row makes the cause obvious at the exact step where it happens.

3

The second slip: handing back too early

Now imagine the answer is returned inside the loop rather than after it. Again nothing crashes. The function stops on its very first item and hands back 60 — which for a one-item basket is even correct, so the bug hides completely until somebody buys two things.

4

Why the tests might have missed both

A single-item basket gives the right answer with either slip present. If your only test was one apple, both bugs pass. This is exactly how quiet mistakes reach real users: the code runs, the output looks like money, and nothing anywhere says otherwise.

5

The habit that pays for itself

Two minutes tracing a two-item basket by hand catches both problems before the program is ever run. Do this a few dozen times and something better happens — you begin seeing what code will do as you read it, which is the difference between typing code and understanding it.

FAQs

Final Words

A function is a box with a promise: state what goes in and what comes back, keep it to one job, and write the smallest logic that keeps the promise. In the playground you connected that design path — and crucially, the by-hand trace that belongs before you ever press run.

The habit worth taking away is the dry run. Pick a real example, write down every value as it changes, and compare the ending with the answer you already know. It catches the quiet bugs that produce plausible numbers, and over time it becomes something more valuable still: the ability to read code and know what it will do.