Functions
A function is a reusable rule.
It takes an input and returns an output.
The notation
means:
f is the name of the rule
x is the input
2x + 1 is how the output is computed
Function Notation
If
then means:
use 3 as the input to f
So:
The expression is not multiplication. It is a function call.
If , what is ?
Compute it first, then check your number.
HintUse 4 as the input
Replace every with 4.
SolutionFunction call
Substitute :
The rule did not change. Only the input changed from the symbol to the
concrete value 4.
The Rule Stays the Same
The input can change. The rule stays the same.
For
we get:
The function gives different outputs because the inputs differ, not because the rule changed.
Let . What is ?
Compute it first, then check your number.
HintSubstitute
Replace with 10.
SolutionComputation
This is the same rule as before. The expression 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.
A function accepts inputs from the set .
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 , 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
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 .
SolutionTrace
The function computes:
With :
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.