A function in 2026 is a named, reusable block of logic you define once and call whenever you need it, optionally passing inputs (parameters) and getting back an output (a return value). To write a good one: give it a clear name that states its job, accept only the inputs it needs, return a predictable result, and keep it focused on a single task. Functions are the building blocks of every program, and small focused ones are easier to read, test, and reuse. Here is how to write them well, with examples.
The anatomy of a function
Every function has the same parts: a name, an optional list of parameters, a body that does the work, and usually a return value. Here is the same simple function in two languages.
// JavaScript: add two numbers and return the result
function add(a, b) {
return a + b;
}
const total = add(2, 3); // total is 5
def add(a, b): # Python: the same idea
return a + b
total = add(2, 3) # total is 5
The parameters a and b are inputs; return sends the result back to whoever called it. That call-and-return pattern is the heart of every function.
Parameters and return values
| Concept |
What it is |
Tip |
| Parameter |
A named input the function expects |
Take only what the function needs |
| Argument |
The actual value you pass in |
Pass clear, valid values |
| Return value |
The output the function hands back |
Return one predictable thing |
| Side effect |
A change outside the function, like printing |
Keep these intentional and few |
| Default value |
A fallback when an argument is missing |
Use to make functions easier to call |
A function that takes clear inputs and returns one clear output is easy to reason about. Functions that quietly change global state or do several unrelated things are where bugs hide. Naming these inputs well is its own skill; see how to name variables.
How to write a good one
- Name it for its job. A function name should be a verb phrase: calculateTotal, sendEmail, isValid. The reader should know what it does without opening the body.
- Give it one responsibility. If you describe it with "and," it is probably two functions.
- Limit the parameters. More than three or four often signals the function is doing too much.
- Return early. Handle invalid input at the top and return, so the main logic is not buried in nesting.
- Keep it short. If it does not fit on a screen, consider splitting it.
- Make it predictable. The same inputs should give the same output where possible; that makes it testable.
Predictable functions are easy to verify; see how to write tests.
What to skip
- Skip functions that do everything. A function named
processData that validates, saves, emails, and logs is four functions wearing a trench coat.
- Skip vague names.
handle, doStuff, and process tell the reader nothing.
- Skip hidden side effects. A function that secretly mutates a global variable surprises everyone who calls it.
- Skip long parameter lists. Group related inputs into an object or struct instead.
- Skip premature cleverness. Clear and slightly longer beats clever and unreadable.
FAQ
What is the difference between a parameter and an argument?
A parameter is the named placeholder in the function definition; an argument is the actual value you pass when you call it. In add(a, b), a and b are parameters; in add(2, 3), 2 and 3 are arguments.
Does a function have to return something?
No. Some functions exist for their side effect, like printing or saving, and return nothing. But a function that computes a value should return it rather than storing it somewhere hidden.
How long should a function be?
Short enough to do one clear thing and fit comfortably on screen. There is no hard line, but if you cannot summarize it in one sentence without "and," it is probably too big.
What makes a function easy to test?
Predictability. A function that takes inputs, returns an output, and avoids hidden side effects can be tested by checking that given inputs produce expected outputs.
Where to go next
Name your variables and functions clearly, write tests for your functions, and write better code overall.