Combining and Composing Functions Advanced. This lesson goes beyond core Algebra I. You can skip it.

Learning goals

  • Add, subtract, multiply and divide two functions, excluding any input where the divisor is zero
  • Compose with (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)), inner first
  • Substitute the whole inner rule to get a composed formula
  • Show that f∘gf \circ g usually differs from g∘fg \circ f
  • Require an input allowed into both functions in turn

Building new functions by arithmetic

Suppose two functions share the same input. Say f(x)=3xf(x) = 3x gives the distance in meters that one runner has covered after xx seconds, and g(x)=x+2g(x) = x + 2 gives a second runner’s distance, starting from a two-meter head start. At any moment xx you can ask about the two runners together: their combined distance is f(x)+g(x)f(x) + g(x), and the difference between their positions is f(x)−g(x)f(x) - g(x). Nothing exotic is happening. You evaluate each function at the same input, then combine the two outputs with ordinary arithmetic. That one idea defines four brand-new functions:

(f+g)(x)=f(x)+g(x),(f−g)(x)=f(x)−g(x),(f + g)(x) = f(x) + g(x), \qquad (f - g)(x) = f(x) - g(x), (fg)(x)=f(x)⋅g(x),(fg)(x)=f(x)g(x).(fg)(x) = f(x) \cdot g(x), \qquad \left(\frac{f}{g}\right)(x) = \frac{f(x)}{g(x)}.

Read the left side of each line as the name of a single new function. The symbol f+gf + g is one function whose rule is “evaluate ff, evaluate gg, and add the results.” To find its output at any input, you do exactly that.

The first three combinations accept every input that both ff and gg accept, so they need no special care. The quotient is the one that does. You already know that division by zero is undefined, so the fraction f(x)g(x)\dfrac{f(x)}{g(x)} has no value at any input where the denominator g(x)g(x) equals zero. Those inputs have to be thrown out, even when ff and gg are both perfectly happy there. So the quotient fg\dfrac{f}{g} is defined wherever ff and gg are both defined and, on top of that, g(x)≠0g(x) \neq 0: it carries the same requirement as the other three combinations, plus one extra ban. This is not a new rule to memorize; it is the old ban on dividing by zero, now applied to the output of gg.

Worked example 1 Adding, subtracting, multiplying, and dividing two functions

Let f(x)=3xf(x) = 3x and g(x)=x+2g(x) = x + 2, now treated simply as algebra rules that accept any real number xx, not only the runner story’s elapsed time. Build all four combinations, and find where the quotient is defined.

For the sum, subtraction, and product, substitute the two rules and simplify:

(f+g)(x)=3x+(x+2)=4x+2,(f + g)(x) = 3x + (x + 2) = 4x + 2,(f−g)(x)=3x−(x+2)=2x−2,(f - g)(x) = 3x - (x + 2) = 2x - 2,(fg)(x)=3x⋅(x+2)=3x2+6x.(fg)(x) = 3x \cdot (x + 2) = 3x^2 + 6x.

For the quotient, write the fraction and then find the input that would make the denominator zero:

(fg)(x)=3xx+2,x+2=0  ⟹  x=−2.\left(\frac{f}{g}\right)(x) = \frac{3x}{x + 2}, \qquad x + 2 = 0 \;\Longrightarrow\; x = -2.

So fg\dfrac{f}{g} is defined for every input except x=−2x = -2. You can also evaluate any combination at a number. At x=4x = 4, for instance, f(4)=12f(4) = 12 and g(4)=6g(4) = 6, so (f+g)(4)=18(f + g)(4) = 18 and (fg)(4)=72(fg)(4) = 72.

Check your understanding

If f(x)=x+6f(x) = x + 6 and g(x)=2xg(x) = 2x, what is (f+g)(x)(f + g)(x)?

Answer choices

Composing: feeding one function into another

Combining with arithmetic keeps the two functions side by side. Composition instead stands them in a line. Picture a small factory with two machines. The first machine takes your raw input and produces a part; the second machine takes that part and finishes it. The output of the first becomes the input of the second.

Make it concrete with g(x)=x+4g(x) = x + 4 and f(x)=x2f(x) = x^2. Start with an input, say 33. Run gg first: g(3)=3+4=7g(3) = 3 + 4 = 7. Now take that result, 77, and run it through ff: f(7)=72=49f(7) = 7^2 = 49. The end product of the two-step process is 4949. Written on one line, this is f(g(3))f(g(3)): the inner function gg acts on 33, and ff acts on whatever gg hands it.

This two-step operation is composition, and it has its own symbol. The composition of ff and gg is written f∘gf \circ g (read ”ff composed with gg,” or ”ff after gg”), and it is defined by

(f∘g)(x)=f(g(x)).(f \circ g)(x) = f(g(x)).

The little ring ∘\circ is not multiplication and not the letter o; it is the composition sign. The right-hand side, f(g(x))f(g(x)), is the form you actually calculate with.

Notice how different this is from the product fgfg from the last section. The product runs ff and gg side by side on the same input and multiplies the two outputs. Composition runs them in a chain: gg‘s output becomes ff‘s input. For f(x)=x+1f(x) = x + 1 and g(x)=2xg(x) = 2x at x=3x = 3, the product is (fg)(3)=f(3)⋅g(3)=4⋅6=24(fg)(3) = f(3) \cdot g(3) = 4 \cdot 6 = 24, but the composition is (f∘g)(3)=f(g(3))=f(6)=7(f \circ g)(3) = f(g(3)) = f(6) = 7. Same two functions, same input, two very different answers.

Which function runs first? The nested notation answers the question on its own. In f(g(x))f(g(x)) the input xx is wrapped inside gg, and g(x)g(x) then sits inside the parentheses of ff. Just as with nested parentheses in arithmetic, you work from the inside out: whatever is deepest gets evaluated first. So gg runs first on the raw input, and ff runs second on gg‘s output. The inner function, the one written closest to the input, always goes first, even though in the name f∘gf \circ g it is written second.

The composition f after g turning the input 3 into 49The input 3 enters box g with rule x plus 4, producing 7, which enters box f with rule x squared, producing the final output 49.inputx = 3gg(x) = x + 4g(3) = 7ff(x) = x²outputf(g(3)) = 49
Composition as a two-machine assembly line. The input enters the inner machine g first, its output becomes the input of the outer machine f, and f produces the final result. Reading f(g(3)) from the inside out matches the order the machines run: g, then f.

Worked example 2 Composing at a number in both orders

Let f(x)=x2f(x) = x^2 and g(x)=x+4g(x) = x + 4. You already found f(g(3))=49f(g(3)) = 49 above by running gg first and then ff. Now reverse the order and compute g(f(3))g(f(3)): run ff on 33 first, then run gg on the result.

f(3)=32=9,g(9)=9+4=13.f(3) = 3^2 = 9, \qquad g(9) = 9 + 4 = 13.

So g(f(3))=13g(f(3)) = 13. Same two functions, same starting number, but 4949 and 1313 are nowhere near each other. The order in which you apply the functions clearly matters, a point the next section makes exact.

Finding a formula for a composition

Evaluating at one input is fine, but usually you want a single formula for f∘gf \circ g that works at every allowed input at once, that is, every input that survives both steps of the composition. The recipe is the substitution you have used since your first algebra lesson. To build f(g(x))f(g(x)), take the entire rule for g(x)g(x) and drop that rule into ff wherever ff expects its input.

Worked example 3 Finding a formula for f∘gf \circ g and g∘fg \circ f

Let f(x)=x2f(x) = x^2 and g(x)=x+4g(x) = x + 4. Find formulas for (f∘g)(x)(f \circ g)(x) and (g∘f)(x)(g \circ f)(x).

For f∘gf \circ g, substitute the whole rule g(x)=x+4g(x) = x + 4 into ff in place of its input, then expand the square. Recall (a+b)2=a2+2ab+b2(a + b)^2 = a^2 + 2ab + b^2, so with a=xa = x and b=4b = 4:

(f∘g)(x)=f(g(x))=f(x+4)=(x+4)2=x2+2(4x)+42=x2+8x+16.\begin{aligned} (f \circ g)(x) &= f(g(x)) = f(x + 4) = (x + 4)^2 \\ &= x^2 + 2(4x) + 4^2 = x^2 + 8x + 16. \end{aligned}

For g∘fg \circ f, substitute the whole rule f(x)=x2f(x) = x^2 into gg in place of its input:

(g∘f)(x)=g(f(x))=g(x2)=x2+4.(g \circ f)(x) = g(f(x)) = g(x^2) = x^2 + 4.

These two formulas are not the same function, and that matches the previous worked example: plugging x=3x = 3 into x2+8x+16x^2 + 8x + 16 and into x2+4x^2 + 4 reproduces the very numbers, 4949 and 1313, found there by running the machines step by step.

Check your understanding

If f(x)=2xf(x) = 2x and g(x)=x+1g(x) = x + 1, what is (f∘g)(x)(f \circ g)(x)?

Answer choices

Check your understanding

If f(x)=x2f(x) = x^2 and g(x)=x−1g(x) = x - 1, what is f(g(4))f(g(4))?

Answer choices

Order matters: a composition is not commutative

Look back at the last two worked examples. In both, swapping the order changed the answer: f(g(3))=49f(g(3)) = 49 but g(f(3))=13g(f(3)) = 13, and the formula f(g(x))=x2+8x+16f(g(x)) = x^2 + 8x + 16 against g(f(x))=x2+4g(f(x)) = x^2 + 4. This is not a fluke of one example. Composition is not commutative: in general f∘gf \circ g and g∘fg \circ f are different functions, and reversing the order usually changes the result. This is the single most important idea in the lesson, so it is worth understanding why it happens rather than just noting that it does.

The reason is exactly the assembly-line picture. In f∘gf \circ g the last machine to touch the number is ff, so the final answer is always something ff produced. In g∘fg \circ f the last machine is gg, so the final answer is always something gg produced. For these two functions ff and gg do different jobs, one squares and one shifts, so the two lines hand you different products. Squaring and then adding four is not the same as adding four and then squaring: g(f(x))=x2+4g(f(x)) = x^2 + 4 puts the four in at the very end. By contrast, f(g(x))=(x+4)2f(g(x)) = (x + 4)^2 adds the four first and then squares the whole thing, which spreads the four through the extra middle term 8x8x. The four enters at a different stage, and the machine that comes after it treats it differently.

Composing in the two orders gives different results, 49 and 13Top line adds four then squares to reach 49; bottom line squares then adds four to reach 13.f(g(3)): first do g, then f3add 47square49g(f(3)): first do f, then g3square9add 413The two results differ, so the order changes the answer.
The same two functions, run in the two possible orders on the input 3, produce 49 and 13. Because the outer machine acts last, whichever function is outer has the final say over the output, so for these two functions, switching the order switches the result.

The domain of a composition

Because a composition runs in two steps, an input has to survive both to be allowed through. Here is a case where that catches an input by surprise.

Worked example 4 The domain of a composition

Let f(x)=1xf(x) = \dfrac{1}{x} and g(x)=x−4g(x) = x - 4. Find (f∘g)(x)(f \circ g)(x) and its domain.

Substitute gg into ff to get the formula:

(f∘g)(x)=f(g(x))=f(x−4)=1x−4.(f \circ g)(x) = f(g(x)) = f(x - 4) = \frac{1}{x - 4}.

Now watch what happens at x=4x = 4. The inner function is perfectly happy there: g(4)=0g(4) = 0, and gg never complains about the input 44. But hand that output, 00, to ff, and f(0)=10f(0) = \dfrac{1}{0} is undefined. So x=4x = 4 looked completely safe going in, yet it is excluded from the domain of f∘gf \circ g. A safe input like x=6x = 6 sails through instead: g(6)=2g(6) = 2 and then f(2)=12f(2) = \dfrac{1}{2}. The domain of f∘gf \circ g is all numbers except 44.

That is not a coincidence of these two particular functions; the same two-step test decided the outcome for 44 above, and it decides every composition’s domain. Here is that test stated exactly.

Which inputs a composition accepts#

To evaluate (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)), an input has to survive two questions asked in order.

Can gg use it? The first step computes g(x)g(x). If xx is not in the domain of gg, then g(x)g(x) does not exist, so there is nothing to hand to ff. The composite f(g(x))f(g(x)) is undefined before ff even gets a turn.

Can ff use what gg produces? Even when g(x)g(x) exists, ff can only act on it if g(x)g(x) is itself an allowed input of ff, that is, if g(x)g(x) lies in the domain of ff. If g(x)g(x) is a value ff rejects, such as a number that would make ff divide by zero, then f(g(x))f(g(x)) is undefined again.

Both questions have to come back yes. An input that fails either one is excluded from the domain of f∘gf \circ g; an input that passes both is included. That two-part rule is nothing new to memorize, it is just the two-step process read out loud.

Check your understanding

Let f(x)=1xf(x) = \dfrac{1}{x} and g(x)=x−7g(x) = x - 7. Which input is NOT in the domain of f∘gf \circ g?

Answer choices

Composition models a two-step process

Composition is not just a symbol game. Any time one quantity depends on a second, which in turn depends on a third, you have a composition waiting to be written down. Discounts followed by fees, readings passed through a converter, a shape whose size grows with time: each is one function feeding the next.

Worked example 5 A discount followed by shipping

An online store takes 2020 percent off every item, then adds a flat shipping fee of 55 dollars to the discounted price. Model the final price as a composition, and find it for an item that normally costs 4040 dollars.

Name the two steps. Taking 2020 percent off leaves 8080 percent of the price, so the discount function is d(p)=0.8pd(p) = 0.8p. Adding shipping to a price is s(x)=x+5s(x) = x + 5. The store discounts first and then adds shipping to the discounted price, so shipping is the outer function and the final price is s(d(p))s(d(p)):

s(d(p))=s(0.8p)=0.8p+5.s(d(p)) = s(0.8p) = 0.8p + 5.

For an item with p=40p = 40:

s(d(40))=0.8(40)+5=32+5=37.s(d(40)) = 0.8(40) + 5 = 32 + 5 = 37.

The final price is 3737 dollars. Order matters here too, exactly as before. Had the store instead added shipping first and then discounted, the result would be d(s(p))=0.8(p+5)=0.8p+4d(s(p)) = 0.8(p + 5) = 0.8p + 4, which at p=40p = 40 gives 3636 dollars. The two policies are genuinely different, and the composition records which one the store actually uses.

Common mistakes

Practice

Multiple Choice Questions (MCQ)

Progressively harder sets of questions. Each opens on its own page.

Core practice

Practice problems at the level of the course, to be worked out on paper. Hints one at a time, then the answer or the full worked solution, with your progress kept in this browser.

Core practice Work it out on paper 10 problems Start →
More practice (optional)

Extra sets, as hard as the Challenge set. Each one opens on its own page.

More resources (optional)

Other explanations of this lesson, if you want a second take.

A bit of history (optional)

Long before Charles Babbage drew up his famous calculating engines, he spent years on a stranger question. What can you do to a function itself?

Ordinary algebra hunts for an unknown number. Babbage wanted an algebra whose unknown was a whole rule. He called the subject the calculus of functions. His first paper on it appeared in 1815, when he was still in his early twenties.

Many of the questions that subject asks only make sense once you are willing to feed a function’s output into a function. That is the move this whole lesson rests on. Composition lets one rule accept another rule’s output as its own input, as long as that output actually lies in its domain, and the parentheses keep the bookkeeping straight.

The engines came later, and they are what his name is remembered for. A machine like that computes by running one step into the next, which is composition in another guise, and a chain has a direction. Feed xx to gg first and hand the result to ff, then compare that to running ff first and handing its result to gg: as in this lesson, the two chains usually land on different answers.