What Is a Function?

Stand in front of a vending machine and press B4. One snack drops out — the one the label promised. Press B4 again tomorrow: the same snack. The machine never hesitates between two snacks, and it never dispenses both at once. Your phone does the same trick: tap a contact's name and it dials exactly one number. Type a postcode into a map and it lands on exactly one neighbourhood. All three are reliable machines — feed in an input, and out comes one definite, repeatable output.

That reliability has a mathematical name. A function is a rule that takes an input and gives back exactly one output — every time. Same input, same output, no exceptions and no moods. This is the single most important object in all of calculus: everything you'll do later — slopes, derivatives, integrals — is done to functions, so it pays to nail down exactly what one is.

We write f(x) for "the output of the function f when the input is x" — read it aloud as "f of x". The letter f names the machine; x is whatever you dropped in. Try it below: move the input and switch the machine's rule — whatever you choose, one input still gives one output.

Worked example: evaluating a function

Evaluating a function is nothing more than running the machine by hand. Say the rule is

f(x) = 3x - 2

and we want f(4). The recipe: wherever the rule says x, substitute 4 — brackets around the input keep you safe when negatives show up.

f(4) = 3(4) - 2 = 12 - 2 = 10

Same machine, different fuel: f(-1) = 3(-1) - 2 = -5, and f(0) = -2. Each input produced exactly one output — the machine never gave us a choice.

Machines can also be chained: the output of one becomes the input of the next. With f(x) = 3x - 2 and a second machine g(x) = x^2, what is g(f(4))? Work from the inside out:

f(4) = 10, \qquad g(f(4)) = g(10) = 10^2 = 100

The input 4 went through both machines and came out as 100 — and because each machine is reliable, the whole chain is reliable too. (Chaining functions like this gets its own name later — composition — and it's the heart of the chain rule in calculus.)

The one rule: one input, one output

That little word exactly is the whole idea. Every input is allowed just one output. A picture called a mapping diagram makes it clear: draw the inputs on the left, the outputs on the right, and an arrow from each input to its output.

It is fine for two inputs to share an output. What is not allowed is one input with two arrows leaving it — then the rule can't decide, so it isn't a function. The test is always about the arrows leaving the left side, never about how many arrive on the right. Try the buttons: the middle one lets inputs b and c share an output (still a function), while the last makes input b sprout a second arrow — which is not.

Worked example: is this table a function?

Functions often arrive as a table of input–output pairs, and you're asked to judge them. The method is always the same: scan the input column for repeats. A repeated input is only a problem if it comes with different outputs.

Table A — inputs and outputs:

1 \to 4, \qquad 2 \to 7, \qquad 3 \to 7

Check each input: 1 appears once, 2 once, 3 once. No input has two outputs, so this is a function. Notice that 2 and 3 both land on 7 — outputs are allowed to be shared. Two arrows arriving at the same place is fine.

Table B — inputs and outputs:

1 \to 4, \qquad 2 \to 7, \qquad 1 \to 9

The input 1 appears twice — once sent to 4, once to 9. Ask the machine for f(1) and it can't decide. This is not a function. (If the repeated input had come with the same output both times — say 1 \to 4 listed twice — it would merely be a duplicate row, and the table would still be a function.)

This asymmetry trips up almost everyone at first, so pin it down now:

The vertical-line test below is precisely this asymmetry drawn on a graph: a vertical line collects all the outputs of one input, so two crossings means one-to-many — illegal. A horizontal line crossing many times only detects many-to-one, which is no crime at all: the parabola y = x^2 fails a horizontal-line test everywhere above the axis and is still a perfectly good function.

The vertical-line test

A graph is just a function's entire table drawn at once: each point (x, y) says "input x, output y". So there's a quick visual check. Sweep a vertical line across the picture: the line x = c touches every point whose input is c. If it ever crosses the curve more than once, that single input would have two outputs — so it is not a function.

Drag the line. A straight line like y = 4 - 2x is caught exactly once, and so is the parabola y = x^2. Switch to x = y^2 and drag past the middle: two crossings appear, and the test fails. Note the test doesn't need a crossing everywhere — a vertical line missing the curve entirely just means that input isn't in the function's domain, which is allowed. The only thing forbidden is a double hit.

For most of history a "function" meant a formula — something you could write down with powers, roots and trig, like y = x^3 - 2x. Then in 1837 the German mathematician Peter Gustav Lejeune Dirichlet quietly liberated the idea: a function, he said, is any rule that assigns one output to each input. No formula required. No pattern required. The assignment itself is the function.

To prove the point he cooked up a genuine monster: the rule that outputs 1 when the input is a fraction (a rational number) and 0 when it isn't. There is no formula for it, and you can't draw its graph — between any two rationals hides an irrational and vice versa, so the "graph" is two infinitely dusty layers of points. Yet it passes the one test that matters: every input gets exactly one output. It is a 100% legitimate function.

This generosity is what makes the modern definition so powerful. "Each person \to their birthday" is a function. "Each moment of today \to the temperature outside" is a function nobody has a formula for. Calculus works on all of them.

Functions in code — the same idea, running

If you've ever programmed, you've already met functions under the same name. Here is f(x) = 3x - 2 as a TypeScript function. Run it — then change the rule or the inputs and run it again. Notice the guarantee: call it twice with the same input and you get the same answer twice. Programmers prize this so much they have a name for it — a pure function.

function f(x: number): number { return 3 * x - 2; } console.log(f(4)); // 10 console.log(f(4)); // 10 again — same input, same output, every time console.log(f(-1)); // -5 // Chain two machines: g(f(4)) function g(x: number): number { return x * x; } console.log(g(f(4))); // 100

A function that isn't reliable — say one that returns a random number, or a different answer depending on the time of day — breaks the mathematical definition, and (not coincidentally) is also the kind programmers find hardest to debug.

See it explained

Sal Khan walks through the same idea — one input, one output — with a handful of quick examples.