Skip to content
Math Foundation Functions & Notation
Lesson 1 ⏱ 8 min

What is a function?

Video coming soon

What Is a Function - The Input-Output Machine

Builds intuition for functions using vending machines and recipes, explains f(x) notation, domain and range, and shows how ML models are just very complicated functions.

⏱ ~5 min

The Input-Output Machine

Here's the most useful way to think about a function: it's a machine. You drop something in one end, and something comes out the other. Every function you'll ever encounter - no matter how complicated - is just a machine that takes inputs and produces outputs.

Functions are the core abstraction of machine learning: a neural network is a function that maps pixel values to category probabilities, a language model is a function that maps text tokens to likely next words, and training is the process of adjusting a function's internal parameters until its outputs match your desired targets.

Think about a vending machine. You press B4, and out comes a cola. You press C2, and out comes chips. The vending machine is a : every button press maps to exactly one product. If pressing B4 sometimes gives you cola and sometimes gives you nothing, that's a broken vending machine - and it's also not a function, because a function must give you exactly one output for each input.

Or think about temperature conversion. To convert Celsius to Fahrenheit, you use:

F=95C+32F = \frac{9}{5} C + 32
FF
Fahrenheit temperature - the output
CC
Celsius temperature - the input

Hand it 0 (freezing), and it outputs 32. Hand it 100 (boiling), and you get 212. One input, one output, every time.

The f(x) Notation - Demystified

Mathematicians write functions like this: . That notation trips people up constantly, so let's kill the confusion right now.

  • f is the name of the function. You could call it g, h, or anything.
  • x is the input. It's a placeholder - a slot waiting to be filled.
  • f(x) means "what the function named f does to input x."

When you write f(3)f(3), you're saying: "feed 3 into function f, what comes out?" The parentheses are not multiplication. f(3)f(3) does not mean f times 3. It means "f evaluated at 3."

So if f(x)=2x+1f(x) = 2x + 1, then:

Domain and Range

Every function has two sets worth knowing about.

The is the set of allowed inputs. Consider f(x)=1/xf(x) = 1/x. You cannot input 0, because dividing by zero is undefined. So 0 is not in the domain. For f(x)=xf(x) = \sqrt{x}, negative inputs are not in the domain (in the real numbers).

The is the set of possible outputs. For f(x)=x2f(x) = x^2, the range is all non-negative numbers - squaring anything gives 0 or something positive.

In plain terms: domain = "what you can put in," range = "what might come out."

Interactive example

Domain and range - drag x along the number line and watch which outputs are reachable for different functions

Coming soon

Functions With Multiple Inputs

Functions can take more than one input. The area of a rectangle depends on both length and width:

A(l,w)=l×wA(l, w) = l \times w
AA
area of the rectangle - the output
ll
length - first input
ww
width - second input

Here, A(3,4)=12A(3, 4) = 12. A(5,7)=35A(5, 7) = 35. Two inputs go in, one number comes out.

You'll see this constantly in ML. A might be L(y,y^)L(y, \hat{y}) - it takes the true label y and the predicted label ŷ, and outputs a number measuring how wrong the prediction was.

Function Composition

You can chain functions together: the output of one becomes the input of the next. If g(x)=x2g(x) = x^2 and f(x)=x+1f(x) = x + 1, then:

(gf)(x)=g(f(x))=g(x+1)=(x+1)2(g \circ f)(x) = g(f(x)) = g(x+1) = (x+1)^2
gfg \circ f
g composed with f - apply f first, then pass result to g
xx
the original input
# In Python, a function is defined with def
def f(x):
    return 2 * x + 1

print(f(3))   # → 7
print(f(10))  # → 21

# Functions with multiple inputs
def loss(y, y_hat):
    """Squared error: how wrong was the prediction?"""
    return (y - y_hat) ** 2

print(loss(5.0, 4.2))  # → 0.64

# Function composition: apply f, then apply g to the result
def square(x): return x ** 2
def add_one(x): return x + 1

composed = lambda x: square(add_one(x))  # g(f(x)) = (x+1)²
print(composed(3))  # → 16

Why This Connects to Machine Learning

A trained image classifier is literally a function. You hand it an image - represented as a grid of pixel values, say 224×224×3=150,528224 \times 224 \times 3 = 150{,}528 numbers - and it outputs a list of probabilities. Something like: "cat: 92%, dog: 6%, bird: 2%."

Every time you run the same image through the same model, you get the same output. One input (the pixel array), one output (the probability vector). That's a function.

When you're training a model, you're searching for the right function. You start with a random function (random weights), measure how wrong it is, and adjust it bit by bit. The mathematics of that adjustment is what the rest of this course is about.

But none of it works without understanding what a function is. And now you do.

Quiz

1 / 3

What does f(3) = 9 mean?