In a table
Look for the same input listed twice with different outputs. Inputs 1, 2, 3 giving 4, 4, 4 is still a function; input 2 giving both 6 and 7 is not.
Free Lesson
A function is a rule that takes an input and gives back exactly one output, like a machine that turns 4 into 9 every single time. In math it is written f(x); in programming it is a named, reusable block of code that takes inputs and returns a result.
Try one below: feed numbers or words into a function machine, see the same rule as math and as code, then crack hidden machines in the guess-the-rule game.
Interactive lab
Pick a machine, type an input and run it. The same rule is shown as math and as Python or JavaScript code, and every run lands in the input-output table. Everything runs in your browser.
Input
?
Rule
Multiply by 2, then add 1
f(x) = 2x + 1
Output
?
f(x) = 2x + 1
Run the machine to fill in a value.
f is the function's name, x is the input, and f(x) is the output.
Run a few inputs to build the table.
A hidden machine is waiting. Feed it inputs, watch the outputs, then pick the rule.
Starter, machine 1 of 2 (takes numbers)
Five questions on f(x), input-output tables, code and the vertical line test.
Two meanings, one idea
| Aspect | In math | In programming |
|---|---|---|
| Looks like | f(x) = 2x + 1 | def f(x): return 2 * x + 1 |
| Input | x, a number from the domain | x, a parameter (the value passed in is the argument) |
| Rule | A formula | Lines of code in the function body |
| Output | f(x), exactly one value per input | The value after return (or nothing) |
| Using it | Substitute: f(3) = 7 | Call it: f(3) returns 7 |
| Extra powers | None, it only maps inputs to outputs | Can also print, save files or change things (side effects) |
Anatomy
def double(x):
return x * 2
print(double(5)) # 10defdouble(x)return x * 2double(5)Is it a function?
Look for the same input listed twice with different outputs. Inputs 1, 2, 3 giving 4, 4, 4 is still a function; input 2 giving both 6 and 7 is not.
Slide a vertical line across the graph. If it ever crosses the graph twice, one x has two y values, so it is not a function. A line or a U-shaped parabola passes; a circle fails.
How to
Step 1
Pick a verb-like name that says what the function does, such as double, shout or to_fahrenheit.
Step 2
Put the parameters in brackets after the name: def double(x): in Python or function double(x) { in JavaScript.
Step 3
In the body, work out the answer and send it back with return, for example return x * 2.
Step 4
Use the name with a real value in brackets, double(5), and the call is replaced by the returned output, 10.
Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.
FAQ
A function is a rule that takes an input and gives back exactly one output. Think of a machine: put 4 into a doubling machine and 8 comes out, every time.
In math, a function pairs every input (x) with exactly one output (y), written f(x). For f(x) = 2x + 1, the input 3 always gives 7. The set of allowed inputs is the domain and the possible outputs are the range.
In programming, a function is a named, reusable block of code. You give it inputs called parameters, it runs its steps, and it can send a value back with return. You run it by calling its name, like double(5).
Check that no input has two different outputs. In a table, look for a repeated input with different outputs. On a graph, use the vertical line test: if any vertical line crosses the graph more than once, it is not a function.
A parameter is the named input slot in the definition, like x in def double(x). An argument is the actual value you pass when you call it, like 5 in double(5).
They share the idea of input, rule and output. Programming functions can also do things besides returning a value, such as printing or saving a file, and some return nothing at all.