Back to Coding

What Is a Function and When Should You Use One?

What Is a Function and When Should You Use One?
AI Tutorials Team 15 March 2026

A function is a named piece of code you can run whenever you want, with different inputs each time. The reason to use one is not cleverness, it is that a script written as one long list becomes impossible to change once it passes about fifty lines.

What are the parts?

A name, some inputs and something it gives back. You call it by name, hand it the inputs, and receive the result. The code inside runs each time, with whatever you supplied.

The inputs are usually called arguments or parameters, and what comes back is the return value.

When should you write one?

  • When you do the same thing twice. The clearest signal.
  • When a section has a name. If you would describe a block as the bit that cleans the dates, it is a function.
  • When a script passes fifty lines. Structure starts paying for itself.
  • When you want to test something separately. A function can be checked on its own.

Why does repetition matter so much?

Because the same code in three places has to be corrected in three places, and somebody will always miss one. That is the origin of a large share of all bugs, and a function removes it entirely.

What do beginners get wrong?

Writing functions that do several things, which defeats the purpose. A function that reads a file, cleans the data and sends an email cannot be reused, tested or understood in isolation, and its name will be a lie.

One function, one job, is a rule worth taking seriously early.

What about returning versus printing?

A common confusion. Printing displays something on screen; returning hands a value back to the code that called it. A function that prints its answer cannot pass it anywhere, which is why generated examples that print instead of returning are frustrating to build on.

How does generated code differ?

It tends to structure into functions earlier and more thoroughly than a beginner would, which is good practice and can be confusing. A twenty-line task split into four functions is harder to follow first time than the same thing written straight through.

Asking for the simple version first, then asking how it would be structured properly, teaches both.

Show me this as one straightforward sequence first so I can follow it. Then show it split into functions, and explain why you split it where you did.

What is the practical benefit for a beginner?

Testing. A function that takes an input and returns a result can be run on its own with a known value, which is the only way to check a piece of logic without running the whole script.

What is the difference between a function and a method?

Terminology that confuses beginners more than it should. A method is a function that belongs to something, called by naming the thing first, which is why some operations look different in style from others.

Practically the distinction matters very little at this stage. Both take inputs and give something back, both are called by name, and the difference is about how the code is organised rather than what it does. Knowing that the two words describe roughly the same idea removes a source of unnecessary confusion when reading explanations.

What is the sign you are ready?

Finding yourself scrolling. When a script is long enough that you scroll to remind yourself what a section does, it needs functions, and the moment you notice that is the right moment to learn them.