Skip to lesson content

Math / Algebra / Lesson 2

Algebra · Learn and experiment

What makes a rule dependable?

Your outcome: Explain functions, permitted inputs and produced outputs without confusing repeated outputs with conflicting answers.

Lesson 2 of 6

Functions, domains and outputs

You used an equation to connect distance and a bill. Now ask what makes that rule usable for every permitted request.

One permitted input, one defined output

A vending machine accepts a selection and follows a rule. A mathematical function does the same: each permitted input has exactly one output. Different inputs may share an output.

Pause and predict

For f(x) = 2x + 1, what comes out when x is 3? Would one input producing two different outputs still define a function?

Need a quick refresher?

An expression calculates a value. An equation compares values. The chosen domain is part of the function's definition.

Review Variables and equations →

The full story · about 12 minutes

A function is a promise: one input, one output

1 · Observe

Autos, fares and a surprise

Words used in this lesson
WordWhat it means
FunctionA rule that gives exactly one output for each input.
DomainThe inputs the rule is allowed to take.
Input and outputWhat goes in, and what comes out.
UndefinedNo answer exists, as with dividing by zero.
Square rootThe non-negative number that squares to the input.

An auto-rickshaw fare is ₹30 plus ₹14 per kilometre. Give the driver a distance and he gives you exactly one fare. That reliability is what mathematicians call a function. If the same distance could produce two different fares, you could not plan a budget.

2 · Express

Function notation

We write f(x) for the output of the rule f at input x. The set of allowed inputs is the domain. The set of outputs actually produced is the range.

fare(km) = 30 + 14 km
domain: km ≥ 0 (a distance cannot be negative)
f(2.5) means: put 2.5 in where the symbol x or km was, and compute.

3 · Visualize

Evaluating, and guarding the domain

fare at three distances
kmfare (₹)
030
2.565
10170

Asking for a fare at −1 km is outside the domain. Code should say so loudly instead of returning a number. The same idea shows up in 1 ÷ (x − 3): any x works except 3, where the denominator is zero. Near 3 the output explodes, at x = 3.001 it is 1,000, and at 3 it does not exist.

A rule that explodes at 3
-10-505100246x = 3: undefinedx1 / (x − 3)
  • left of 3
  • right of 3
See the numbers behind this picture
A rule that explodes at 3
xleft of 3right of 3
0-0.333–
0.1-0.345–
0.2-0.357–
0.3-0.37–
0.4-0.385–
0.5-0.4–
0.6-0.417–
0.7-0.435–
0.8-0.455–
0.9-0.476–
1-0.5–
1.1-0.526–
1.2-0.556–
1.3-0.588–
1.4-0.625–
1.5-0.667–
1.6-0.714–
1.7-0.769–
1.8-0.833–
1.9-0.909–
2-1–
2.1-1.111–
2.2-1.25–
2.3-1.429–
2.4-1.667–
2.5-2–
2.6-2.5–
2.7-3.333–
2.8-5–
2.9-10–
3.1–10
3.2–5
3.3–3.333
3.4–2.5
3.5–2
3.6–1.667
3.7–1.429
3.8–1.25
3.9–1.111
4–1

4 · Discover

Not every rule is a function

The question 'what number squares to 4?' has two answers, +2 and −2. One input, two outputs: not a function. The square-root function fixes this by promising the non-negative answer, so √4 = 2 every time.

5 · Apply

Two worked examples

Example 1, a fare. fare(2.5) = 30 + 14 × 2.5 = 65. The same input always gives 65.

Example 2, a domain. g(x) = √(x − 1). For x = 5, g = 2. For x = −3 the square root of −4 is not a real number, so the domain is x ≥ 1.

Check yourself

Which is NOT a function of x?

6 · Where it goes wrong

Three ways it goes wrong

7 · In Python

A fare function with guards

Here is how to implement it, step by step. The code below does exactly these steps, and every line is editable.

  1. Write the rule as a function with a name, such as fare(km).
  2. State the domain: for fare, km must be zero or more.
  3. Make the code refuse inputs outside the domain, loudly.
  4. For a division, find the input that makes the denominator zero and exclude it.
  5. Test the edges: just inside and just outside the domain.
Evaluate, guard the domain, spot a non-function
import math

def fare(km):
    """Auto fare: rupees. Defined only for a distance of zero or more."""
    if km < 0:
        raise ValueError("distance cannot be negative")
    return 30 + 14 * km

for km in (0, 2.5, 10):
    print("fare(", km, ") =", fare(km))

try:
    fare(-1)
except ValueError as e:
    print("fare(-1) ->", e)

# a rule that is not a function of x alone: one input, two outputs
pairs = [(4, 2), (4, -2)]            # x = 4 gives both +2 and -2 as 'square roots'
print("inputs repeated with different outputs:", {x for x, _ in pairs if sum(1 for a, _ in pairs if a == x) > 1})
print("the function sqrt picks one:", math.sqrt(4))

# the domain of 1 / (x - 3): everything except 3
for x in (2, 3.001, 4):
    print("1 / (x - 3) at", x, "=", round(1 / (x - 3), 3))
try:
    1 / (3 - 3)
except ZeroDivisionError:
    print("1 / (3 - 3) -> undefined, 3 is outside the domain")
fare( 0 ) = 30
fare( 2.5 ) = 65.0
fare( 10 ) = 170
fare(-1) -> distance cannot be negative
inputs repeated with different outputs: {4}
the function sqrt picks one: 2.0
1 / (x - 3) at 2 = -1.0
1 / (x - 3) at 3.001 = 1000.0
1 / (x - 3) at 4 = 1.0
1 / (3 - 3) -> undefined, 3 is outside the domain
Edit and run this code in your browser

Live Python

Evaluate, guard the domain, spot a non-function

Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

Predict, then explore

Before changing anything: Compare mappings (1,4),(2,4) and (2,4),(2,7). Which breaks the promise? Predict before changing the lookup mapping.

Use the Lookup mapping selector to compare shared outputs with conflicting outputs. Separately change the input and Function rule; read the resulting f(x) and the matching table row.

Playground

Feed the fare machine

A delivery app quotes f(d) = 2d + 3 rupees for d km. One input in, exactly one quote out, but only for distances the app serves (0 to 30 km). Try a negative distance.

0102030-404812distance d (km)fare f(d) (₹)
A 2.5 km trip costs ₹8.
Input d
2.5 km
Output f(d)
₹8
In the domain?
yes

The domain is the set of inputs the rule accepts. Outside it, the machine does not return a wrong number: it returns no quote at all.

The general function explorer

Function Explorer

f(x) = 2x + 3. At x = 2, output = 7.

Mapping: (1, 4), (2, 4). This is a function: each input has one output. Domain: 1, 2. Range: 4.

Input xOutput f(x)−44
Curves join sampled values; they are visual guides, not a proof between samples. Axes rescale for the selected rule. Use the table for exact values; a domain gap has no plotted value.
See the exact values behind the graph
Runtime function samples
Input xSelected output
-4-5
-3-3
-2-1
-11
03
0.54
15
27
39
411

In your own words: what changed, and why did the output change with it?

Now you drive

Write a function with a domain

A Python def is a function in exactly the maths sense: one input in, one output out. The domain becomes a guard that refuses inputs the rule does not accept.

Maths and Python, side by side
MathsPythonIn plain words
f(d) = 2d + 3return 2 * d + 3The rule.
domain: 0 ≤ d ≤ 30if d < 0 or d > 30: raise ValueErrorRefuse anything outside it.

Live Python

fare(d) with a guarded domain

Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

Stuck? Show a worked solution (try first!)
def fare(d):
    """Delivery fare in ₹ for d km. The app serves 0 to 30 km only."""
    if d < 0 or d > 30:
        raise ValueError("outside the domain")
    return 2 * d + 3

for d in (0, 2.5, 7):
    print(f"{d} km -> ₹{fare(d)}")
try:
    fare(-1)
except ValueError as err:
    print("-1 km ->", err)

Experiment, then explain

Finish the TODOs, then try the quick experiments. Think about each question before opening its answer.

Why raise an error instead of returning 0 for a bad distance?

A 0 looks like a real quote. An error cannot be mistaken for an answer, which is how a function stays honest about what it does not cover.

Inputs 1, 3 and 5 pass through f(x) = 2x + 1 to outputs 3, 7 and 11.
Inputs 1, 3 and 5 pass through f(x) = 2x + 1 to outputs 3, 7 and 11.
Open full-size figure for One permitted input, one defined output (opens a new tab)↗
One permitted input, one defined output: follow the quantities
Input xRuleOutput f(x)
12 × 1 + 13
32 × 3 + 17
52 × 5 + 111

Writing f(x) names the result of applying f to x; it does not mean f multiplied by x. The domain tells us which inputs the rule permits. 1/(x−2) excludes 2; a 40-seat bus also imposes whole-number passenger counts from 0 to 40.

Relate the playground’s rule to this reading

A function assigns exactly one output to each input in its domain. The domain is the set of allowed inputs; the range is the outputs those inputs produce. Different inputs may share an output.

For f(x) = 2x + 3 and domain {0,1,2}, the range is {3,5,7}. f(2) means apply the rule at input 2, giving 7. The function name is not another multiplier.

A domain can come from the problem or arithmetic. A ticket count may need to be a nonnegative integer; 1/x excludes zero because division by zero is undefined. A calculator's willingness to accept a number does not make it meaningful in context.

Use f for the rule and f(x) for its output. Domain and range belong to the stated function, not just the formula printed on screen.

Symbol and unit reference
f, f(x)
Function name and its output at x
domain
Permitted input set
range
Outputs produced by the permitted inputs
f(g(x))
Apply g, then f
Another worked example

For f(x) = 2x + 3, f(2) = 7. Pairs (1,4) and (2,4) are a function: both inputs have one output. Pairs (2,4) and (2,7) are not: the same input has conflicting outputs.

Connect the relationship to Python

def delivery_bill(distance_km):
    return 2 * distance_km + 3

quote = delivery_bill(2)  # 7

def names a reusable rule, the argument supplies an input, and return supplies its output. This short arithmetic example omits production input validation; the allowed domain must still be specified.

Why this matters for ML

A preprocessing function must produce a well-defined output for each supported record. An out-of-domain record needs an explanation, not a made-up number.

Try a different situation

A lookup maps product A to 10 rupees and product B to 10 rupees. Is that a function? What if product A also maps to 12 rupees under the same rule?

Compare your reasoning

Shared prices are allowed: each product still has one output. Assigning two different prices to the same product under the same stated rule violates the function definition. A time or pricing tier could instead become an additional input if the context requires it.

Not scored or saved. Compare your reasoning, not just the answer.

Read a larger expression when you’re ready

The five-level Equation X-Ray connects these ideas to multiple inputs, curves, nested functions and indexed sums. Choose any level; advanced solving is not required.

Equation X-Ray · Read the structure, then explain it

Mathematics as a language

Start with a familiar rule and build toward a larger expression. Reading its pieces comes before solving advanced equations. You can explore any level.

One rule, many possibilities

A trip has a 3-rupee booking fee and costs 2 rupees per kilometre. How can one statement describe any permitted distance?

y = 2x + 3

Read the relationship before trying to solve it. This is an illustrative example, separate from the live experiment.

Choose a building block to inspect its role.

Read the operations: Multiply distance by its rate, then add the fee. If a bill is known, solving asks which distance makes both sides equal.

Build the expression from smaller operations

Choose a valid construction in the stated order. This exercise organizes dependencies; it does not claim every implementation must evaluate independent branches in this order.

    What problem could this relationship represent?

    A booking fee plus a distance charge is one possible interpretation; a fixed production setup cost plus a per-item charge has the same structure.

    Context we still need: Symbols alone do not establish currency, physical meaning or whether negative distances are allowed.

    Connect the operations to code
    bill = 2 * distance_km + 3

    Read the code as a map of operations. It is not executed here; imports, shapes and numerical safeguards depend on the actual lesson.

    Explain a changed situation

    Your notes and reveals are temporary. This reading practice does not change lesson completion or saved assessment evidence.

    Understanding checks

    Use the worked reasoning above. Answers within 0.001 count as correct.

    1. Is this complete lookup mapping a function: (1,4), (2,4)?
    2. For f(x)=2x+3, calculate f(2).
    3. The rule h(x)=1/x is evaluated over real numbers. Which input must be excluded?
    4. For f(x)=2x+3 with domain {0,1,2}, which set is its range?

    Carry this idea forward

    A useful function needs an input contract as well as a calculation.

    Next, put the input-output pairs on axes so the relationship becomes visible.

    What the existing checks cover
    • Identify a valid function without confusing repeated outputs with conflicting outputs.
    • Interpret function notation and calculate an output.
    • Identify input restrictions before evaluating a rule.
    • Find the outputs produced by a stated domain.

    Build your lesson evidence

    Try the experiment. 0 of 4 checks done.