Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 · Functions and Transformations

Composition of Functions

Learn to compose functions, evaluate f(g(x)), find the domain of a composite, decompose a function into two simpler ones and see why order matters.

Often the output of one process becomes the input of another. A thermometer reads Celsius, a converter turns it into Fahrenheit, and a display rounds the result. Each step is a function, and chaining them is called composition.

The composition of f and g is written (f ∘ g)(x) and means f(g(x)): apply g first, then apply f to the result. The notation reads right to left, which is easy to forget. The inner function is closest to x, so it acts first.

Evaluating a composite

Example 1. Let f(x) = 2x + 3 and g(x) = x². Find f(g(4)). First g(4) = 16. Then f(16) = 2(16) + 3 = 35. Now g(f(4)): first f(4) = 11, then g(11) = 121. So f(g(4)) = 35 but g(f(4)) = 121.

Order matters. In general f(g(x)) ≠ g(f(x)). Putting on socks then shoes is not the same as shoes then socks.

Finding a formula

Substitute the whole inner function in place of x.

Example 2. With the same f and g: f(g(x)) = f(x²) = 2x² + 3. And g(f(x)) = g(2x + 3) = (2x + 3)² = 4x² + 12x + 9. Compare them at x = 4: the first gives 2(16) + 3 = 35 and the second gives 4(16) + 48 + 9 = 121. They match the numbers above.

Example 3. Let f(x) = √x and g(x) = x - 5. Then f(g(x)) = √(x - 5). And g(f(x)) = √x - 5. Different functions, different graphs.

Python Compose functions in code

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Domain of a composite

f(g(x)) is defined when two things hold: x is in the domain of g, and g(x) is in the domain of f.

Example 4. f(x) = √x and g(x) = x - 5. For f(g(x)) = √(x - 5) we need x - 5 ≥ 0, so the domain is [5, ∞). For g(f(x)) = √x - 5 we need only x ≥ 0, so the domain is [0, ∞).

Example 5. f(x) = 1/(x - 2) and g(x) = x + 1. f(g(x)) = 1/((x + 1) - 2) = 1/(x - 1). Domain: all reals except 1. Simplifying first can hide a restriction, so also check the inner function's domain. Here g accepts every real, so 1 is the only exclusion.

Python Find where a composite is defined

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Decomposing a function

Going backward, you can break a complicated function into an inner and outer part. For h(x) = (3x - 1)⁴, take the inner function g(x) = 3x - 1 and the outer function f(x) = x⁴. Then h(x) = f(g(x)). For h(x) = √(x² + 1), take g(x) = x² + 1 and f(x) = √x.

Decomposition is not unique, but there is usually a natural choice: the "inside" is whatever you would compute first on a calculator.

Composition with the identity

The identity function I(x) = x does nothing: f(I(x)) = f(x) and I(f(x)) = f(x). This idea returns in the next lesson, where an inverse function is the one that composes with f to give the identity.

flowchart LR
    X["x"] -- "g" --> G["g(x)"]
    G -- "f" --> F["f(g(x))"]

Composition chains two machines: the output of g feeds f.

Real-world composition

A store gives 20% off a 50 dollar item, then adds 8% tax. Discount function: d(p) = 0.8p. Tax function: t(p) = 1.08p. Final price: t(d(50)) = 1.08 × 40 = 43.20 dollars. Here the order happens not to matter because both are multiplications, but with a flat 5 dollar coupon and tax the order would change the total.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. If f(x) = x + 1 and g(x) = 3x, what is f(g(2))?

    g(2) = 6, then f(6) = 7.
  2. 2. If f(x) = x² and g(x) = x - 1, what is g(f(x))?

    g(f(x)) = g(x²) = x² - 1. The other two expressions are f(g(x)).
  3. 3. What is the domain of f(g(x)) when f(x) = √x and g(x) = 2x - 6?

    We need 2x - 6 ≥ 0, so x ≥ 3.
  4. 4. Which pair decomposes h(x) = (x + 4)³?

    Compute x + 4 first (inner), then cube it (outer).

Summary

  • (f ∘ g)(x) = f(g(x)): apply g first, then f.
  • Composition is generally not commutative: f(g(x)) ≠ g(f(x)).
  • To get a formula, substitute the entire inner function for x.
  • The domain of f(g(x)) needs x in the domain of g and g(x) in the domain of f.
  • Many functions can be decomposed into an inside and an outside part.

Next: Inverse Functions