Objective
Learning objectives
- Define a function as a rule that maps each input to exactly one output
- Read and write function notation
f(x) - Recognize the difference between a function and a relation that isn't one
- Implement a function in Python and call it with different inputs
Hook
Given how many tokens you send to an AI model, what will it cost? Ask that question for 500 tokens, and there's one answer. Ask it for 50,000 tokens, and there's a different single answer — but still just one. That's the entire idea this lesson names: a predictable process where every input has exactly one legitimate answer.
See it
Watch the machine run three different inputs through the same rule and produce three single, predictable outputs — that's the normal case, and it's the entire job of a function. Then watch it jam: fed one input, it tries to hand back two different outputs at once, and the whole idea breaks. That jam isn't a bug in the figure — it's the exact reason mathematicians bothered to define "function" as its own word in the first place: to separate rules that behave predictably from ones that don't.
A quick, informal test
If you can imagine feeding the same input in twice and, depending on mood, getting two different valid outputs — it's not a function. "Today's weather" isn't a function of "the date" the way 2x + 1 is a function of x, because two different years can share a date (March 3rd) with completely different weather. A real function can't do that, ever, by definition.
Name it
Function notation writes a function as f(x) — read "f of x" — where f names the rule and x names the input. f(x) = 2x + 1 is the exact same rule as the figure above; f(2) means "run the rule with input 2," which gives 5.
The input is what goes in (also called the argument or domain value); the output is what comes out (also called the value or range value). The defining property of a function — worth stating precisely, since "the same input always gives the same output" is easy to nod along to without noticing its full weight — is that every valid input maps to exactly one output. Not zero, not two: exactly one, every single time.
Code it
def cost(tokens):
return (tokens / 1000) * 0.002
print(cost(500))
print(cost(500))
print(cost(50000))
0.001
0.001
0.1
This is function notation — cost is f, tokens is x, and cost(500) is exactly f(500). Notice the first two lines: calling cost(500) twice, with nothing else changed, produces the identical result both times. That's not a coincidence the code happens to get right — it's a Python function structurally incapable of doing anything else, which is exactly the property that makes it a faithful implementation of a mathematical function rather than just code that happens to compute something.
def double_plus_one(x):
return 2 * x + 1
print(double_plus_one(2))
print(double_plus_one(10))
print(double_plus_one(-3))
Same rule (2x + 1), three different inputs, three single outputs — 2 → 5, 10 → 21, -3 → -5. This is the identical f(x) = 2x + 1 machine from See It, just written as literal Python instead of animated. Nothing about the rule changes between calls; only the input does.