A vending machine is a function. You put in a code, say B4, and out comes one specific snack, every single time. Press B4 again tomorrow and you get the same snack. You never press B4 and get two snacks, or a different one each time.
That's the whole idea of a function in maths: a rule that takes an input and gives back exactly one output. Functions are everywhere once you look: converting Celsius to Fahrenheit, working out a taxi fare from the distance, calculating tax from income. And a machine learning model is a function too: features go in, a prediction comes out.
This lesson explains functions from scratch: the input-rule-output idea, the f(x) notation that confuses many beginners, domain and range, combining functions, and how all of it maps directly onto Python.
What you need first: variables and expressions.
Key terms
- Function: a rule that gives exactly one output for each input.
- Input: the value you put in, usually called x. Also called the argument.
- Output: the value you get back, usually called y or f(x).
- f(x): read "f of x": the output of function f when the input is x.
- Domain: all the inputs a function accepts.
- Range: all the outputs it can produce.
- Composition: feeding the output of one function into another.
The function machine
Picture a machine with a slot on one side and a chute on the other:
input → [ rule ] → output
Take the rule "double it, then add 1":
| Input | Rule | Output |
|---|---|---|
| 0 | 2 × 0 + 1 | 1 |
| 1 | 2 × 1 + 1 | 3 |
| 2 | 2 × 2 + 1 | 5 |
| 3 | 2 × 3 + 1 | 7 |
| −1 | 2 × (−1) + 1 | −1 |
Same input, same output, every time. That reliability is what makes something a function.
f(x) notation
Writing "the double-it-and-add-1 rule" gets tiring, so we give the function a name, usually a single letter like f, and write:
f(x) = 2x + 1
Read it as "f of x equals 2x plus 1". The most important thing to know: f(x) does not mean f times x. The brackets hold the input. It's exactly like calling a function in code.
- f(3) means "the output when the input is 3": f(3) = 2(3) + 1 = 7.
- f(−1) = 2(−1) + 1 = −1.
- f(a) = 2a + 1. The input can even be another letter.
The letter x is just a placeholder. f(x) = 2x + 1 and f(t) = 2t + 1 describe the same function.
Functions can have other names: g(x), h(t), or descriptive ones like cost(d) or area(r). Because y is the output, you'll also see y = 2x + 1, which means the same thing.
Functions in Python
A Python function is a maths function you can run. The notation is almost identical:
def f(x):
return 2 * x + 1
for x in [0, 1, 2, 3, -1]:
print(x, "->", f(x))
0 -> 1
1 -> 3
2 -> 5
3 -> 7
-1 -> -1
Maths writes f(3); Python writes f(3). The def line names the function and its input; the return line is the rule. Python functions covers the programming side in depth.
What is (and isn't) a function
The rule: each input gives exactly one output. Two different inputs giving the same output is fine; one input giving two outputs is not.
| Relationship | Function? | Why |
|---|---|---|
| Person → their date of birth | Yes | Everyone has exactly one birth date |
| Date of birth → person | No | Many people share a birthday |
| x → x² | Yes | 3 and −3 both give 9, but each input has one output |
| Student → their exam score | Yes | One score per student per exam |
| Number → its square roots | No | 9 has two square roots, 3 and −3 |
This is why √ is defined to give only the positive root: so that it's a function. On a graph, you can check with the vertical line test, explained in how to read any graph.
Domain and range
The domain is the set of inputs a function can accept. The range is the set of outputs it actually produces.
| Function | Domain | Range | Why |
|---|---|---|---|
| f(x) = 2x + 1 | all numbers | all numbers | any number works in and can come out |
| f(x) = x² | all numbers | 0 and above | squares are never negative |
| f(x) = √x | 0 and above | 0 and above | no real square root of a negative |
| f(x) = 1/x | all numbers except 0 | all numbers except 0 | you can't divide by zero |
In real problems, the domain is often limited by common sense. A taxi fare function cost(d) = 50 + 15d only makes sense for d ≥ 0: you can't ride −3 km.
In code, going outside the domain gives errors:
import math
print(math.sqrt(16))
try:
print(math.sqrt(-16))
except ValueError as error:
print("Error:", error)
4.0
Error: math domain error
Python even calls it a domain error.
Functions with several inputs
A function can take more than one input, but it still gives one output. The area of a rectangle depends on two inputs:
area(l, w) = l × w
So area(5, 3) = 15. A linear regression model is a function of many inputs: price(area, bedrooms, age) = one predicted price.
Applying a function to a whole column
In data science, you rarely apply a function to one number. You apply it to thousands at once. With pandas and NumPy, writing the rule for a whole column looks just like writing it for one value:
import pandas as pd
celsius = pd.Series([0, 25, 37, 100])
fahrenheit = celsius * 1.8 + 32
print(fahrenheit.round(1).tolist())
[32.0, 77.0, 98.6, 212.0]
The function F(C) = 1.8C + 32 was applied to every value in one line. This is called vectorisation, and NumPy is built around it.
Combining functions: composition
You can feed one function's output straight into another. If g(x) = x − 32 and h(x) = x / 1.8, then:
h(g(x)) = (x − 32) / 1.8
That's Fahrenheit to Celsius, built from two simple steps. Work from the inside out: do g first, then h.
def g(x):
return x - 32
def h(x):
return x / 1.8
print(h(g(212)))
print(h(g(98.6)))
100.0
36.99999999999999
36.99999999999999 is 37 with a tiny rounding error, because computers store decimals like 98.6 only approximately (see decimals and rounding). Wrap the result in round(..., 1) when displaying it.
Order matters: g(h(212)) = 212/1.8 − 32 ≈ 85.8, a different answer.
Composition is how complex models are built. A neural network is many simple functions composed together, one layer feeding the next, and logistic regression is a straight-line function fed into an S-shaped function.
Functions in machine learning
Machine learning, in one sentence: finding a function from examples.
- You have inputs (features, like a house's area) and outputs (labels, like its price).
- You don't know the rule connecting them.
- A model is a family of functions with adjustable numbers, like f(x) = mx + c with m and c unknown.
- Training picks the numbers that make f(x) match the examples best.
- Predicting is just calculating f(x) for a new input.
Everything else in this module, graphs, slopes, curve shapes and inverses, is about understanding what these functions look like and how they behave.
Practice
- If f(x) = 3x − 4, find f(2), f(0) and f(−3).
Show the answer
f(2) = 2, f(0) = −4, f(−3) = −13.
- If g(x) = x² + 1, find g(4) and g(−4). Does g give the same output for two different inputs? Is it still a function?
Show the answer
Both are 17. Yes, that's allowed: each input still has exactly one output, so g is a function.
- Is "a country → its capital city" a function? What about "a city → the country it's in", if you count cities with the same name in different countries?
Show the answer
Country → capital is a function in most cases (one capital each). City name → country is not: a name like "Hyderabad" maps to both India and Pakistan.
- What is the domain of f(x) = 1/(x − 2)?
Show the answer
All numbers except 2, because x = 2 would mean dividing by zero.
- If f(x) = 2x and g(x) = x + 3, find f(g(1)) and g(f(1)).
Show the answer
f(g(1)) = f(4) = 8. g(f(1)) = g(2) = 5. Different orders give different answers.
Key takeaways
- A function is a rule giving exactly one output for each input.
- f(x) means "the output of f for input x", not f times x.
- The domain is the allowed inputs; the range is the possible outputs.
- Python functions, vectorised pandas columns and ML models are all functions.
- Composition chains functions: work from the inside out, and order matters.
Frequently asked questions
What does f(x) mean?
It means "the value of the function f when the input is x". For f(x) = 2x + 1, f(3) is 7. The brackets hold the input; they don't mean multiplication.
What is the difference between domain and range?
The domain is the set of inputs a function accepts; the range is the set of outputs it can produce. For f(x) = x², the domain is all numbers and the range is zero and above.
Why is a machine learning model called a function?
Because it takes inputs (features) and returns one output (a prediction) for each, following a fixed rule once trained. Training is the process of choosing that rule from examples.
What to learn next
- Draw functions on a grid in coordinates and the Cartesian plane.
- Revisit quadratic equations explained.