Functions

A function is a reusable rule.

It takes an input and returns an output.

The notation

f(x)=2x+1f(x) = 2x + 1

means:

f is the name of the rule
x is the input
2x + 1 is how the output is computed
input3rule2x + 1output7
A function maps an input to an output by applying a rule.

Function Notation

If

f(x)=2x+1f(x) = 2x + 1

then f(3)f(3) means:

use 3 as the input to f

So:

f(3)=23+1=7f(3) = 2 \cdot 3 + 1 = 7

The expression f(3)f(3) is not multiplication. It is a function call.

MATH-C01-T03-001Exercise: Call the function

If f(x)=x2+1f(x) = x^2 + 1, what is f(4)f(4)?

Compute it first, then check your number.

HintUse 4 as the input

Replace every xx with 4.

SolutionFunction call

Substitute x=4x = 4:

f(4)=42+1=16+1=17f(4) = 4^2 + 1 = 16 + 1 = 17

The rule did not change. Only the input changed from the symbol xx to the concrete value 4.

The Rule Stays the Same

The input can change. The rule stays the same.

For

f(x)=2x+1f(x) = 2x + 1

we get:

f(0)=1f(0) = 1 f(3)=7f(3) = 7 f(10)=21f(10) = 21

The function gives different outputs because the inputs differ, not because the rule changed.

MATH-C01-T03-002Exercise: Use the same rule twice

Let f(x)=2x+1f(x) = 2x + 1. What is f(10)f(10)?

Compute it first, then check your number.

HintSubstitute

Replace xx with 10.

SolutionComputation
f(10)=210+1=21f(10) = 2 \cdot 10 + 1 = 21

This is the same rule as before. The expression f(10)f(10) says to use 10 as the input, not to multiply f by 10.

Domain and Range

The domain is the set of inputs the function accepts.

The range is the set of outputs the function can produce.

For this chapter, keep the idea simple:

domain: what can go in
range: what can come out

If a function accepts whole numbers from 0 to 10, then -3 is outside that domain. The rule may still be written down, but that input is not allowed for that function as defined.

MATH-C01-T03-003Exercise: Check the domain

A function accepts inputs from the set {0,1,2,3}\{0, 1, 2, 3\}.

Enter 1 if 2 is in the domain.

Compute it first, then check your number.

HintRead the set

The domain is the list of allowed inputs.

SolutionMembership

Enter 1. The value 2 appears in the set {0,1,2,3}\{0, 1, 2, 3\}, so it is in the domain.

Functions as Transformations

Functions are not only formulas. They are transformations.

A model layer is a function. A loss is a function. A tokenizer is a function in the practical sense: it takes text and produces tokens.

The habit is the same:

input -> rule -> output

Code Mirror

def f(x):
    return 2 * x + 1

print(f(3))

This prints:

7
MATH-C01-T03-004Exercise: Read the code as a function

What does this code print?

def f(x):
    return x * x + 1

print(f(5))

Compute it first, then check your number.

HintFunction call

f(5) means run the rule with x=5x = 5.

SolutionTrace

The function computes:

x2+1x^2 + 1

With x=5x = 5:

52+1=25+1=265^2 + 1 = 25 + 1 = 26

The code mirrors the mathematical function call: put the input into the rule, then return the computed output.

Common Doubt

Is every function a line or curve on a graph?

No. Graphs are one way to picture some functions. In ML, a function may map text to tokens, tokens to vectors, vectors to logits, or logits to probabilities. The important habit is to ask what goes in, what rule is applied, and what comes out.

Next, we need a way to talk about collections of objects. That is where sets enter.