Function Composition: Functions Inside Functions
You encrypt a message. Then you encrypt the result again with a different key.
You compress a file. Then you convert it to base64.
You take input, process it, then process the output with a different function.
That's composition.
You feed the output of one function into the input of another. The result is a new function that chains the two processes together.
The Unlock: Functions Eat Functions
Functions take numbers and produce numbers.
But they can also take the outputs of other functions and produce numbers.
If f(x) = x² and g(x) = x + 1, then f(g(x)) means: apply g first, then apply f to the result.
g(3) = 4. Now apply f: f(4) = 16.
So f(g(3)) = 16.
Composition is the arithmetic of functions. Instead of adding or multiplying numbers, you're chaining processes.
Notation: f(g(x)) vs. (f ∘ g)(x)
There are two ways to write composition.
Nested notation: f(g(x)). Apply g, then apply f.
Composition operator: (f ∘ g)(x). Read "f composed with g of x."
They mean the same thing.
(f ∘ g)(x) = f(g(x)).
The circle ∘ is the composition operator. It combines functions the way + combines numbers.
Order Matters
f(g(x)) is not the same as g(f(x)).
Example: f(x) = x², g(x) = x + 1.
f(g(x)) = f(x + 1) = (x + 1)² = x² + 2x + 1.
g(f(x)) = g(x²) = x² + 1.
These are different functions.
Order matters. Composition is not commutative.
How to Compute a Composition
Method 1: Substitute.
Write g(x). Replace every instance of x in f(x) with g(x).
Example: f(x) = 2x + 3, g(x) = x².
f(g(x)) = 2(g(x)) + 3 = 2x² + 3.
Method 2: Evaluate inside out.
Compute g(x) first. Then plug the result into f.
Example: f(x) = √x, g(x) = x - 1. Find f(g(5)).
g(5) = 4.
f(4) = 2.
So f(g(5)) = 2.
Method 3: Build the formula.
Let y = g(x). Then f(g(x)) = f(y). Substitute y = g(x) back in.
This is useful when the composition is complex.
Domain of a Composition
The domain of f(g(x)) is not simply the domain of g.
You need two conditions:
- x must be in the domain of g.
- g(x) must be in the domain of f.
Example: f(x) = √x, g(x) = x - 1.
Domain of g: all real numbers.
Domain of f: [0, ∞).
For f(g(x)) = √(x - 1) to be defined, you need g(x) ≥ 0, so x - 1 ≥ 0, so x ≥ 1.
Domain of f(g(x)): [1, ∞).
The composition's domain is the most restrictive of the two constraints.
Composing More Than Two Functions
You can chain as many functions as you want.
f(g(h(x))): apply h, then g, then f.
Example: f(x) = x + 1, g(x) = 2x, h(x) = x².
h(3) = 9.
g(9) = 18.
f(18) = 19.
So f(g(h(3))) = 19.
To build the formula:
h(x) = x².
g(h(x)) = 2x².
f(g(h(x))) = 2x² + 1.
Read from the inside out.
Decomposing Functions
Sometimes you want to break a complex function into simpler pieces.
Given F(x) = √(x + 1), can you write it as a composition?
Let g(x) = x + 1. Let f(x) = √x.
Then F(x) = f(g(x)).
Decomposition is the reverse of composition. You're finding the building blocks.
Example: F(x) = (2x + 3)³.
Let g(x) = 2x + 3. Let f(x) = x³.
Then F(x) = f(g(x)).
Decomposition is useful in calculus. The chain rule requires you to identify the inner and outer functions.
Why Composition Matters: Building Complexity
Simple functions are easy to understand. But real-world processes are rarely simple.
Composition lets you build complex functions from simple ones.
Example: A company's profit depends on sales. Sales depend on price. Price depends on demand.
profit = f(sales) sales = g(price) price = h(demand)
The composite function profit = f(g(h(demand))) describes how profit depends on demand, even though the relationship passes through intermediate variables.
Composition is how you model multi-stage processes.
Identity Function
The identity function is f(x) = x. It returns the input unchanged.
The identity function is the "do nothing" function.
Composing with the identity does nothing:
f(I(x)) = f(x).
I(f(x)) = f(x).
The identity is the neutral element of composition, like 0 is the neutral element of addition or 1 is the neutral element of multiplication.
Composition and Inverses
If f and f⁻¹ are inverses, then:
f(f⁻¹(x)) = x.
f⁻¹(f(x)) = x.
Composing a function with its inverse gives the identity function.
Example: f(x) = 2x, f⁻¹(x) = x/2.
f(f⁻¹(x)) = 2(x/2) = x.
f⁻¹(f(x)) = (2x)/2 = x.
The function and its inverse undo each other.
Composition Is Associative
(f ∘ (g ∘ h))(x) = ((f ∘ g) ∘ h)(x).
You can group compositions however you want. The result is the same.
This means you can write f ∘ g ∘ h without ambiguity.
Proof:
(f ∘ (g ∘ h))(x) = f((g ∘ h)(x)) = f(g(h(x))).
((f ∘ g) ∘ h)(x) = (f ∘ g)(h(x)) = f(g(h(x))).
Same result.
Associativity is important. It means composition has nice algebraic properties.
Composition and Transformations
Transformations are compositions.
f(x - 2) is the composition of f with g(x) = x - 2.
2f(x) is the composition of h(y) = 2y with f.
Scaling, shifting, reflecting—they're all compositions with simple functions.
This is why transformations compose cleanly. Under the hood, they're function compositions.
Iterating a Function
Composing a function with itself is called iteration.
f(f(x)) is denoted f²(x). (Not to be confused with (f(x))², which is f(x) squared.)
f(f(f(x))) is f³(x).
Iteration is central to dynamics, chaos theory, and recursion.
Example: f(x) = 2x. Start with x = 1.
f(1) = 2.
f²(1) = f(f(1)) = f(2) = 4.
f³(1) = f(f(f(1))) = f(4) = 8.
The sequence: 1, 2, 4, 8, 16, ... It grows exponentially.
Different starting points and different functions produce different behaviors: fixed points, cycles, chaos.
Fixed Points
A fixed point of f is a value x such that f(x) = x.
At a fixed point, the function leaves the input unchanged.
Example: f(x) = x². The fixed points satisfy x² = x, so x(x - 1) = 0, giving x = 0 or x = 1.
f(0) = 0. f(1) = 1. Both are fixed points.
If x is a fixed point of f, then f²(x) = f(f(x)) = f(x) = x. The point is fixed under iteration.
Fixed points are central to studying dynamical systems.
Linear Composition
When f and g are linear, composition is straightforward.
Let f(x) = ax + b and g(x) = cx + d.
f(g(x)) = a(cx + d) + b = acx + ad + b.
The composition is also linear: f(g(x)) = (ac)x + (ad + b).
The slope of the composition is the product of the slopes: ac.
The intercept of the composition combines the intercepts: ad + b.
Linear functions compose cleanly.
Exponential and Logarithmic Composition
Exponentials and logarithms are inverses. They compose to give the identity.
f(x) = e^x, g(x) = ln(x).
f(g(x)) = e^(ln(x)) = x.
g(f(x)) = ln(e^x) = x.
This identity is used constantly in calculus and algebra.
Similarly, f(x) = 10^x and g(x) = log₁₀(x) are inverses.
Trigonometric Composition
Composing trig functions with polynomials creates oscillatory functions with modified periods and amplitudes.
Example: f(x) = sin(2x).
This is the composition of g(x) = 2x with h(x) = sin(x).
The factor 2 inside the sine doubles the frequency. The period changes from 2π to π.
Example: f(x) = sin(x + π/2).
This is the composition of g(x) = x + π/2 with h(x) = sin(x).
The shift by π/2 phase-shifts the sine wave. sin(x + π/2) = cos(x).
Composition modifies the behavior of trig functions.
Composition in Real-World Models
Temperature conversion: Celsius to Fahrenheit is F = (9/5)C + 32. Fahrenheit to Kelvin is K = (F - 32)(5/9) + 273.15.
To go directly from Celsius to Kelvin, compose: K = C + 273.15.
Unit conversion chains: Meters to feet to inches. Each step is a function. The composition converts directly from meters to inches.
Multi-stage processes: Manufacturing involves cutting, assembling, painting. Each stage is a function. The composition describes the entire process.
Encryption: Encrypt with key 1, then encrypt again with key 2. The composition is a double-encrypted message.
Composition models sequential processes.
Graphical Interpretation
To visualize f(g(x)), you can trace through the composition graphically.
- Start with input x.
- Find g(x) on the graph of g.
- Use g(x) as the input to f. Find f(g(x)) on the graph of f.
This is cumbersome, but it gives intuition.
Alternatively, graph f(g(x)) directly by computing values and plotting.
When Composition Simplifies
Sometimes composition creates a simpler function.
Example: f(x) = √x, g(x) = x².
For x ≥ 0:
f(g(x)) = √(x²) = x.
The composition is the identity function.
Example: f(x) = e^x, g(x) = ln(x).
f(g(x)) = e^(ln(x)) = x.
Again, the identity.
Composition with inverses always simplifies to the identity.
When Composition Gets Messy
Composing complicated functions can produce messy results.
Example: f(x) = (x + 1)/(x - 1), g(x) = x².
f(g(x)) = (x² + 1)/(x² - 1).
Still manageable.
But if you keep composing, formulas explode.
That's why decomposition is useful. Breaking functions into simpler pieces makes them easier to work with.
Composition and the Chain Rule
In calculus, the chain rule computes the derivative of a composition.
If F(x) = f(g(x)), then F'(x) = f'(g(x)) · g'(x).
You multiply the derivative of the outer function (evaluated at the inner function) by the derivative of the inner function.
Understanding composition is essential for understanding the chain rule.
Example: F(x) = (x² + 1)³.
Let g(x) = x² + 1, f(x) = x³.
F(x) = f(g(x)).
g'(x) = 2x, f'(x) = 3x².
F'(x) = f'(g(x)) · g'(x) = 3(x² + 1)² · 2x = 6x(x² + 1)².
Composition and the chain rule go hand in hand.
Composition as a Binary Operation
In algebra, a binary operation takes two inputs and produces one output.
Addition: a + b. Multiplication: a · b.
Composition: f ∘ g.
Composition is a binary operation on functions.
It's associative: (f ∘ g) ∘ h = f ∘ (g ∘ h).
It has an identity: f ∘ I = I ∘ f = f.
It has inverses (for invertible functions): f ∘ f⁻¹ = f⁻¹ ∘ f = I.
These properties make the set of invertible functions under composition a group—a fundamental structure in abstract algebra.
Composition in Programming
In programming, function composition is a core concept in functional programming.
You define small, reusable functions. Then you compose them to build complex operations.
Example (pseudocode):
f(x) = x * 2
g(x) = x + 3
h = compose(f, g) // h(x) = f(g(x)) = 2(x + 3)
Composition avoids intermediate variables. It's a clean, declarative style.
Languages like Haskell make composition a first-class operation. The . operator composes functions.
Visualizing Composition: Function Machines
Imagine functions as machines.
g is a machine. You feed in x, it spits out g(x).
f is another machine. You feed in g(x), it spits out f(g(x)).
Composition is connecting the output of g to the input of f.
x → [g] → g(x) → [f] → f(g(x)).
The composition (f ∘ g) is a single machine that does what g and f do sequentially.
x → [f ∘ g] → f(g(x)).
This mental model makes composition intuitive.
Common Mistakes
Mistake 1: Confusing f(g(x)) with f(x)·g(x).
f(g(x)) is composition. f(x)·g(x) is multiplication. They're different.
Mistake 2: Getting the order backwards.
f(g(x)) applies g first, then f. Not f first, then g.
Mistake 3: Ignoring domain restrictions.
The domain of f(g(x)) is not the domain of g. You need g(x) to be in the domain of f.
Mistake 4: Treating composition as commutative.
f(g(x)) ≠ g(f(x)) in general. Order matters.
Mistake 5: Confusing f²(x) with (f(x))².
f²(x) = f(f(x)). (f(x))² = f(x) · f(x). Different notations, different meanings.
Why Composition Is Fundamental
Mathematics is about building complex structures from simple ones.
Composition is how you build complex functions from simple ones.
You start with basic functions: polynomials, exponentials, trig functions. You compose them. You get everything else.
Composition is the algebraic structure underlying transformations, the chain rule, dynamical systems, and functional programming.
It's not just a technique. It's a way of thinking: functions as objects you can combine, not just formulas you evaluate.
The Payoff: Seeing Processes as Pipelines
When you understand composition, you stop seeing functions as isolated formulas.
You start seeing them as stages in a pipeline. Data flows through, transformed at each step.
That shift—from static formulas to dynamic processes—is what composition gives you.
It's the shift from arithmetic to functional thinking. And it's essential for everything that comes after.
Part 4 of the Precalculus series.
Previous: Function Transformations: Shifting Stretching Reflecting Next: Inverse Functions: Undoing What a Function Does
Comments ()