A function is a named, reusable block of code that takes inputs, does one job, and usually returns a result. Think of it as a small machine: you feed it ingredients (the inputs), it does its work, and it hands back something (the output). Functions are the most fundamental building block of programming because they let you write a piece of logic once and use it everywhere, instead of copying the same code across your project.
How a function works
A function has three parts: a name, a set of parameters (the inputs), and a body (the work). When you call the function and pass in arguments, the body runs and a value comes back.
-- a function with one parameter and a return value
def square(number):
return number * number
result = square(5) -- result is now 25
Here number is the parameter, 5 is the argument you passed, the body multiplies it, and 25 is the return value. The same idea in JavaScript:
// arrow function: same job, different syntax
const square = (number) => number * number;
const result = square(5); // 25
Why functions matter
| Benefit |
What it means in practice |
| Reuse |
Write logic once, call it from many places |
| Single source of truth |
Fix a bug in one spot, not ten copies |
| Readability |
A good name explains intent without comments |
| Testability |
You can test one function in isolation |
| Composition |
Build big behavior by combining small functions |
The repetition point is the big one. If you compute tax in five places by hand and the rate changes, you must find and fix five copies. Put it in a calculateTax function and you change one line. This habit of naming small units of work is one of the first things learning to code as a beginner should build.
Key terms you will see
- Parameter vs argument. A parameter is the placeholder in the definition; an argument is the actual value you pass when calling.
- Return value. What the function hands back. A function with no return still runs its body; it just gives back nothing useful.
- Side effect. Anything a function does besides returning a value, such as printing, writing a file, or changing a global. Useful but worth keeping deliberate.
- Pure function. A function whose output depends only on its inputs and that has no side effects. Pure functions are the easiest to test and reason about.
How to write a good function
- Make it do one thing. A function named
saveUser should save a user, not also send an email and log analytics. Split those out.
- Name it after what it does.
getActiveUsers beats process. The name should let a reader skip the body.
- Keep the parameter list short. More than three or four inputs usually means the function is doing too much or should take an object.
- Prefer returning a value to changing a global. Easier to test, harder to break by accident.
- Handle the edge cases at the top. Check for empty input or invalid arguments early and return, rather than nesting deeply.
Common mistakes
- The mega-function. A 200-line function that fetches, transforms, validates, and saves is hard to read and test. Break it into named steps.
- Hidden side effects. A function called
getPrice that also writes to the database surprises everyone. Name honestly or split.
- Unclear returns. Returning sometimes a number and sometimes
null or false forces every caller to guess. Be consistent.
What to skip
- Premature abstraction. Do not wrap a single line you use once in a function just because you can; wait until you actually repeat it.
- Clever one-liners. A dense expression nobody can read is worse than three clear lines.
- Comments that restate the code. A well-named function rarely needs a comment explaining what it does.
FAQ
What is the difference between a function and a method?
A method is a function that belongs to an object or class and usually operates on that object data. A plain function stands on its own. The mechanics are the same.
Do functions have to return something?
No. Some functions exist for their side effect, like printing or saving. In many languages a function with no explicit return gives back a default empty value.
What is a parameter versus an argument?
The parameter is the name in the function definition. The argument is the real value you supply when you call it.
How long should a function be?
There is no hard limit, but if you cannot see the whole function on one screen or describe it in one sentence, it is probably doing too much.
Where to go next
See what a variable is in programming, what a design pattern is, and how to debug code faster.