Functions and Function Composition: Rules, Inverses and Pipelines

A complete, plain-language guide with worked examples, diagrams, tables and links to AI and machine learning

In this article
  1. Introduction
  2. What is a function?
  3. Ways to represent a function
  4. Domain and range
  5. The line tests
  6. Common function families
  7. Inverse functions
  8. What is function composition?
  9. Order matters
  10. Domains and decomposing
  11. Real-world chains
  12. AI and machine learning
  13. Common mistakes
  14. Practice problems
  15. Conclusion

1. Introduction

Variables hold values, equations tie them together, and functions turn that structure into something you can use over and over. A function is a rule that takes an input and produces exactly one output. A vending machine is a good picture: press B4 and you always get the same snack. You can understand it without knowing what happens inside.

Once you have functions, you can connect them. Feed the output of one into the next and you build a pipeline. That is function composition, and it matters far beyond algebra: a modern neural network is nothing but a long composition of simple functions.

This article covers what functions are, how to represent them, what inputs they allow, how to reverse them, and how to compose them. We end with how composition drives deep learning, including why a network needs more than linear steps.

2. What is a function?

A function is a rule that assigns to each allowed input exactly one output. We write y = f(x), read "y equals f of x." Here x is the input, f is the name of the rule, and f(x) is the output. The brackets do not mean multiplication. They mean "apply f to what is inside."

Function machine turning input 4 into output 11
A function as a machine: one input in, one output out.

Take f(x) = 2x + 3. To evaluate f(4), replace every x by 4, giving 2(4) + 3 = 11. The input can be a negative number, a fraction, or even another expression such as f(a + 1) = 2(a + 1) + 3 = 2a + 5.

Evaluating f(x) = 2x + 3
Input xSubstitutionOutput f(x)
-22(-2) + 3-1
-12(-1) + 31
02(0) + 33
12(1) + 35
22(2) + 37
32(3) + 39
42(4) + 311
Function vocabulary
TermMeaningExample
Inputthe value you feed in (independent variable)x = 4
Outputthe value that comes out (dependent variable)f(4) = 11
Domainall allowed inputsall real numbers
Rangeall outputs that actually occurall real numbers
Rulewhat the function does to the inputdouble, then add 3

The key requirement is that each input gives only one output. Different inputs may share an output, but one input may never produce two.

A table that is a function, and one that is not
InputOutput (function)InputOutput (not a function)
1414
2727
3729

The left table is fine, because two different inputs (2 and 3) may share the output 7. The right table breaks the rule because the input 2 is paired with both 7 and 9, so the machine would not know which to return.

3. Ways to represent a function

The same function can be shown in four ways, and moving between them is a valuable skill. An equation is compact and exact. A table lists sample values. A mapping diagram draws arrows from inputs to outputs. A graph shows the whole behaviour at once, with each point an (input, output) pair. Words, such as "double the input and add 3," are often the starting point in real problems.

Four representations of f(x) = 2x + 3: equation, table, mapping diagram and graph
Four views of the same function.
Strengths of each representation
RepresentationBest forLimitation
Equationexact values and calculationshape is not obvious
Tableseeing specific valuesonly some inputs shown
Mapping diagramchecking one output per inputimpractical for many inputs
Graphoverall shape and trendsreading exact values is hard

4. Domain and range

The domain is the set of inputs a function accepts, and the range is the set of outputs it produces. Domains are limited by two common troubles: you cannot divide by zero, and you cannot take the square root of a negative number (in real numbers). For f(x) = √(x − 2), the expression under the root must be at least zero, so x ≥ 2. Because a square root is never negative, the range is y ≥ 0.

Graph of the square root of x minus 2 with domain and range marked
Domain and range of √(x − 2).
Domain and range of common functions
FunctionDomainRangeReason
2x + 3all real numbersall real numbersno restrictions
x²all real numbersy ≥ 0squares are never negative
√xx ≥ 0y ≥ 0no root of a negative
1/xx ≠ 0y ≠ 0cannot divide by zero
|x|all real numbersy ≥ 0absolute value is never negative

5. The line tests

How do you tell whether a graph shows a function? Use the vertical line test. If any vertical line crosses the graph more than once, one input has two outputs, so it is not a function. A parabola passes, while a circle fails because a vertical line cuts it in two places.

A related horizontal line test checks whether a function is one-to-one, meaning no two inputs share an output. The curve y = x² fails because both 2 and −2 give 4, while y = x³ passes. Only one-to-one functions can be reversed cleanly, which matters in the next section.

Four graphs illustrating the vertical and horizontal line tests
Vertical line test (top) and horizontal line test (bottom).

6. Common function families

A handful of function families appear again and again. Recognising their shapes and formulas lets you recognise them in the wild, and several of them are the topics of the next articles in this series.

Common function families
FamilyFormulaShape or role
Linearf(x) = mx + bstraight line, constant rate
Quadraticf(x) = ax² + bx + cparabola, one turning point
Absolute valuef(x) = |x|V shape
Square rootf(x) = √xhalf-parabola on its side
Reciprocalf(x) = 1/xtwo branches, never touches the axes
Exponentialf(x) = bˣrapid growth or decay
Logarithmicf(x) = log xslow growth, inverse of exponential
Sigmoidf(x) = 1/(1 + e⁻ˣ)S-curve squeezing values into 0 to 1
ReLUf(x) = max(0, x)zero for negatives, identity for positives

Some functions use different rules on different parts of the domain. These are piecewise functions. The absolute value, |x|, equals x when x ≥ 0 and −x otherwise. ReLU is the same idea: max(0, x) outputs 0 for negative inputs and x for the rest, so ReLU(−3) = 0 and ReLU(5) = 5. Piecewise definitions are how a simple formula can still produce a bend or a corner.

7. Inverse functions

An inverse function undoes the original. If f turns 4 into 11, then f⁻¹ turns 11 back into 4. To find it, write y = f(x), swap x and y, and solve for y. The notation f⁻¹ means "inverse," not "one over f."

Finding the inverse of f(x) = 2x + 3
StepWork
1. Write y =y = 2x + 3
2. Swap x and yx = 2y + 3
3. Solve for yy = (x − 3) / 2
4. Name itf⁻¹(x) = (x − 3) / 2

The check is composition, covered next: f(f⁻¹(x)) = 2 · (x − 3)/2 + 3 = x. The graphs of a function and its inverse are mirror images across the line y = x, because every point (a, b) on one becomes (b, a) on the other.

Graphs of f and its inverse reflected across the line y equals x
Swapping inputs and outputs reflects the graph across y = x.
f and f⁻¹ undo each other
xf(x) = 2x + 3f⁻¹ of that
030
151
272
5135

Only one-to-one functions have inverses. The function x² fails the horizontal line test, so to reverse it we restrict the domain to x ≥ 0, where the inverse is √x.

8. What is function composition?

Function composition means feeding the output of one function into another. The composite of f and g is written (f ∘ g)(x) = f(g(x)). Read it from the inside out: apply g to x first, then apply f to the result. The small circle is read "composed with" or "after."

Let f(x) = 2x + 3 and g(x) = x². Then (f ∘ g)(x) = f(x²) = 2x² + 3. At x = 2, first g(2) = 4, then f(4) = 11. As a check, the formula gives 2(4) + 3 = 11.

Two pipelines showing composition in both orders for input 2
Composition is a pipeline: the order of the stages matters.

9. Order matters

Composition is not commutative: f ∘ g and g ∘ f are usually different. Reversing the order above gives (g ∘ f)(x) = g(2x + 3) = (2x + 3)², and at x = 2 this is f(2) = 7 then 7² = 49, not 11. Think of getting dressed: socks then shoes is a very different outcome from shoes then socks.

The two orders compared, f(x) = 2x + 3 and g(x) = x²
xg(x)(f ∘ g)(x) = 2x² + 3f(x)(g ∘ f)(x) = (2x + 3)²
-2411-11
-11511
00339
115525
2411749
3921981
Curves of f composed with g and g composed with f
The two orders give two different curves.

Money shows the same effect. A shop takes 10 dollars off, then adds 10 percent tax. With d(p) = p − 10 and t(p) = 1.1p, discounting first gives t(d(p)) = 1.1p − 11, while taxing first gives d(t(p)) = 1.1p − 10. For a 100 dollar item that is 99 versus 100 dollars.

Composition properties
PropertyStatementTrue?
Commutativef ∘ g = g ∘ fusually not
Associative(f ∘ g) ∘ h = f ∘ (g ∘ h)always
Identityf ∘ I = I ∘ f = f, where I(x) = xalways
Inversef ∘ f⁻¹ = f⁻¹ ∘ f = Iwhen f⁻¹ exists

10. Domains and decomposing

For (f ∘ g)(x) to make sense, two things must hold: x must be allowed in g, and the result g(x) must be allowed in f. Take f(x) = √x and g(x) = x − 4. The composite (f ∘ g)(x) = √(x − 4) requires x − 4 ≥ 0, so its domain is x ≥ 4. The other order, (g ∘ f)(x) = √x − 4, only needs x ≥ 0.

Domains of composites for f(x) = √x and g(x) = x − 4
CompositeFormulaDomain
f ∘ g√(x − 4)x ≥ 4
g ∘ f√x − 4x ≥ 0

The reverse skill is decomposition: spotting the inner and outer function inside a formula. Ask what happens to x first, then what happens to that result.

Decomposing a formula into f(g(x))
Formula h(x)Inner g(x)Outer f(u)
(3x − 1)²3x − 1u²
√(x² + 1)x² + 1√u
1/(x + 5)x + 51/u
e²ˣ2xeᵘ

Compositions can chain three or more functions. With h(x) = x + 1, g(x) = 2x and f(x) = x², we get f(g(h(x))) = (2(x + 1))². At x = 2: h = 3, then g = 6, then f = 36.

11. Real-world chains

Unit conversions are compositions. Celsius to Fahrenheit is F(C) = 1.8C + 32, and Fahrenheit to Kelvin is K(F) = (F − 32)/1.8 + 273.15. Composing them gives K(F(C)) = C + 273.15, which is the direct Celsius-to-Kelvin rule. The messy middle step simplifies away, a typical gain from composing.

A conversion chain: Celsius → Fahrenheit → Kelvin
C (°C)F(C) (°F)K(F(C)) (K)C + 273.15
-40-40233.15233.15
032273.15273.15
3798.6310.15310.15
100212373.15373.15

Pricing, physics and data pipelines work the same way: each stage is a function, and the whole process is their composition. Spotting that structure lets you analyse, test and replace one stage at a time.

12. AI and machine learning

A single artificial neuron is a composition of two functions. First a linear function mixes the inputs, z = wx + b. Then an activation function such as sigmoid or ReLU reshapes the result. A layer repeats this for many neurons, and a network stacks layers, so the model is ŷ = f₃(f₂(f₁(x))): composition, just deeper.

A tiny two-layer network drawn as a chain of functions
A two-layer network is a composition of four functions.
Forward pass through ŷ = σ(3·ReLU(2x − 3) − 1)
Stagex = 2x = 1
input x21
linear: 2x − 31-1
ReLU10
linear: 3a − 12-1
sigmoid σ0.8810.269

Why not use only linear functions? Because composing linear functions gives another linear function. With f(x) = 2x + 1 and g(x) = 3x − 2, the composite is f(g(x)) = 2(3x − 2) + 1 = 6x − 3. A hundred linear layers collapse into one, and the model can only draw straight lines. Inserting a non-linear activation between layers makes the composite bend, which is what lets networks capture curves, shapes and patterns.

Left: two linear functions compose into one straight line. Right: ReLU in between bends the line
Without a non-linear activation, depth adds no power.
Why activations matter
NetworkCompositeShape
linear then linear6x − 3straight line
linear, ReLU, linear3·ReLU(2x − 1) − 2bent line, flat then rising

Depth is simply the number of functions in the chain. Small networks compose a handful of layers, while large language and vision models compose dozens or hundreds, each a simple function on its own. The power comes from the composition. Early stages detect simple patterns, later stages combine them, and the final stages turn them into an answer, just as g feeds f in our small examples.

Training also depends on composition. To improve the weights we need to know how the output changes when an early value changes. The chain rule from calculus says the slope of a composite is the product of the slopes of its stages: d/dx f(g(x)) = f′(g(x)) · g′(x). Backpropagation applies this stage by stage, multiplying from the output back toward the input. This is calculus rather than algebra, but it only works because networks are compositions.

Chain rule for h(x) = (3x − 1)² at x = 1
StageValue or slopeCalculation
inner g(x) = 3x − 123(1) − 1
slope of g3g′(x) = 3
outer f(u) = u², slope at u = 242u = 2(2)
slope of h124 × 3
The same ideas, two vocabularies
AlgebraMachine learning
functionmodel, layer or activation
input / outputfeatures / prediction
composition f(g(x))stacked layers
sigmoid, ReLU (function families)activation functions
inverse functiondecoding, undoing a transform
domain restrictionvalid input range, preprocessing

13. Common mistakes

Frequent mistakes and corrections
MistakeWrongRight
Reading f(g(x)) left to rightf(g(x)): apply f firstapply g first, then f
Treating composition as multiplicationf(g(x)) = (2x + 3)·x²f(g(x)) = 2x² + 3
Reading f⁻¹ as a reciprocalf⁻¹(x) = 1/(2x + 3)f⁻¹(x) = (x − 3)/2
Assuming f(a + b) = f(a) + f(b)f(x) = x²: f(1 + 2) = 5f(3) = 9, but f(1) + f(2) = 5
Ignoring the domain√(x − 4) at x = 2needs x ≥ 4
Skipping the vertical line testcalling a circle a functiona vertical line hits it twice

14. Practice problems

Try these before looking at the answers.

  1. If f(x) = 3x − 4, find f(5) and f(−2).
  2. State the domain and range of g(x) = √(x + 6).
  3. With f(x) = x + 5 and g(x) = 2x, find (f ∘ g)(x) and (g ∘ f)(x).
  4. With f(x) = x² − 1 and g(x) = x + 2, evaluate (f ∘ g)(3).
  5. Find the inverse of f(x) = (x + 7)/3.
  6. Write h(x) = (x − 4)³ as f(g(x)).
  7. With f(x) = √x and g(x) = x − 9, find the domain of f ∘ g.
  8. Show that f(x) = 2x + 1 composed with g(x) = 3x − 2 equals 6x − 3.

Answers.

  1. f(5) = 3(5) − 4 = 11 and f(−2) = 3(−2) − 4 = −10.
  2. Domain x ≥ −6, range y ≥ 0.
  3. (f ∘ g)(x) = 2x + 5 and (g ∘ f)(x) = 2(x + 5) = 2x + 10.
  4. g(3) = 5, then f(5) = 25 − 1 = 24.
  5. Swap: x = (y + 7)/3, so y = 3x − 7. The inverse is f⁻¹(x) = 3x − 7.
  6. g(x) = x − 4 and f(u) = u³.
  7. √(x − 9) needs x − 9 ≥ 0, so the domain is x ≥ 9.
  8. f(g(x)) = 2(3x − 2) + 1 = 6x − 4 + 1 = 6x − 3.

15. Conclusion

A function is a rule with one output for every allowed input, and it can be shown as an equation, a table, a mapping or a graph. The domain and range describe what goes in and what comes out, the line tests tell you whether a graph is a function and whether it can be reversed, and the inverse undoes it.

Composition chains functions into pipelines. Its order matters, its domain depends on both parts, and in machine learning it is the structure of every neural network, with non-linear activations preventing the whole stack from collapsing into a single line. Next in this series come exponential and logarithmic functions, which are themselves inverses and are the engines of growth, probability and loss functions.