StoicBuilder

HandbookI the LudusFunctions

Ithe Ludus, Read code

Functions

1 min readUpdated 28 November

In plain words

A function is a named block of code that takes some inputs, does one job with them and hands back a result whenever you call it.

Where you see it

When your agent says it "added a formatPrice helper", that's a function. It's defined once, then called on the menu, at checkout and in the order email, each time with a different price.

src/lib/format.tsbrowser
export function formatPrice(cents: number) { return "$" + (cents / 100).toFixed(2);} formatPrice(1250); // "$12.50"
Defined once with an input called cents. Called anywhere with a real number.

What goes wrong

  1. Forgetting the return

    The function does the work, then hands back undefined. The page shows a blank, or the word undefined, where the price should be.

    function addTax(price) { price * 1.2; }
  2. Leaving off the brackets

    formatPrice names the function and formatPrice(1250) runs it. Without the brackets nothing runs, and the function itself gets passed around instead of its result.

  3. One function doing five jobs

    A 200-line function that saves the order, charges the card and sends the email is hard to read and harder to fix. Ask for one function per job.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Before adding a new function, check whether one already exists that does the same job, and reuse it. For each function you add or change, tell me its inputs, what it returns and where it's called.

Quick check

Sign in to earn +10 XP for it

QuickBite's receipt shows the total as "undefined". The calculateTotal function adds up every item. Likely bug?

Got a prompt that touches functions?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.