Basics

What is a function in programming — and why AI breaks code without them

Illustration: a box with one inlet and one outlet standing among tangled wires

Functions usually get explained as «so you don't write the same thing twice». True, but that's the dullest and least important benefit. The real reason is different: a function lets you give a piece of work a name and forget what's inside it. And here's the twist — that's exactly what decides whether AI can fix your code later, or breaks it on every edit.

What a function is, in plain words

A function is a named piece of code that does one job: it takes something in and hands a result back.

Think of a coffee machine. You put in beans and water, press a button, get coffee. What happens inside — grinding, heating, pressure — isn't your problem in that moment. The machine has an input, an output and a name («make an espresso»).

A function is exactly that. You describe how the work is done once. After that you call it by name.

How it's built: input → work → output

Take a task: calculate a discounted price.

function priceWithDiscount(price, percent) {
  const discount = price * percent / 100;
  return price - discount;
}

priceWithDiscount(1000, 20);  // 800

Piece by piece:

  • function — we're declaring one.
  • priceWithDiscount — the name. The most important part; we'll come back to it.
  • (price, percent) — the parameters: what the function takes in.
  • The body in braces — the actual work.
  • return — what it hands back. Without return it computes and silently throws the result away.

The last line is the call. We passed 1000 and 20, we got 800.

The rule for a good function: one function, one job. If the name needs an «and» in it (checkAndSave), that's two functions.

What they're really for

Saving lines is a pleasant side effect. Three real reasons:

1. You stop holding everything in your head. Reading priceWithDiscount(1000, 20), you understand the whole line without looking inside. Your brain handles one concept instead of five operations.

2. A bug lives in one place. The discount formula is written once. Find a bug, fix that one function, and it's gone everywhere. A formula copied into six places gets fixed six times, and you'll forget two.

3. It's testable. A function with a clear input and output is easy to check: give it 1000 and 20, expect 800. A fragment buried mid-file can't be checked like that.

Why this matters so much with AI

Here's the part that makes the boring theory worth it.

When you ask AI to fix code, it works in chunks. The model sees your file through its context window and edits whole logical pieces. How your code is sliced decides everything.

If the code is cut into functions, the task reads: «fix the discount in priceWithDiscount». The model changes eight lines. It leaves the rest alone — there's nothing there to disturb.

If you have a 600-line slab where the discount, the email sending and the markup are interleaved, the model has to rewrite a large chunk to change one formula. On the way it'll disturb something that worked. You get the classic «I asked for one thing and something else broke».

That isn't the model being difficult. A human in its place would do the same.

The practical takeaway for vibe coding: once a file passes a couple hundred lines, ask the AI to split it into functions with clear names — that's refactoring. Then ask for edits to one named function at a time. Surprise breakages drop sharply, because you narrowed the blast radius.

How is a function different from a method?

A method is a function that lives inside an object or class and works with its data. Same mechanics: input, work, output.

Does a function have to return something?

No. Some exist for the action — write a file, send an email. But if a function computed something, that result almost always belongs in a return.

How big should a function be?

Small enough that you can describe its job in one phrase without «and». There's no exact line count, but if it doesn't fit on a screen, there are probably two functions hiding in it.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →