This site is a work in progress. New lessons are added regularly. Contact us

Combining and Composing Functions

Learning goals

  • Add, subtract, multiply and divide two functions at a shared input
  • Exclude inputs where the divisor function is zero
  • Compose with (fg)(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 fgf \circ g usually differs from gfg \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 metres 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-metre 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 gap between them 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),(fg)(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 only where g(x)0g(x) \neq 0. 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. 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,(fg)(x)=3x(x+2)=2x2,(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 fgf \circ g (read ”ff composed with gg,” or ”ff after gg”), and it is defined by

(fg)(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.

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 fgf \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. Compute f(g(3))f(g(3)) and g(f(3))g(f(3)).

For f(g(3))f(g(3)), work from the inside out. Run gg on 33 first, then run ff on the result:

g(3)=3+4=7,f(7)=72=49.g(3) = 3 + 4 = 7, \qquad f(7) = 7^2 = 49.

So f(g(3))=49f(g(3)) = 49. Now reverse the order. For g(f(3))g(f(3)), run ff on 33 first, then run gg:

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 fgf \circ g that works at every input at once. 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 fgf \circ g and gfg \circ f

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

For fgf \circ g, substitute the whole rule g(x)=x+4g(x) = x + 4 into ff in place of its input, then expand the square:

(fg)(x)=f(g(x))=f(x+4)=(x+4)2=x2+8x+16.(f \circ g)(x) = f(g(x)) = f(x + 4) = (x + 4)^2 = x^2 + 8x + 16.

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

(gf)(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. As a quick check, evaluate each at x=3x = 3: (fg)(3)=9+24+16=49(f \circ g)(3) = 9 + 24 + 16 = 49 and (gf)(3)=9+4=13(g \circ f)(3) = 9 + 4 = 13, exactly the two numbers from the previous worked example.

Check your understanding

If f(x)=x2f(x) = x^2 and g(x)=x1g(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 fgf \circ g and gfg \circ f are different functions, and reversing the order 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 fgf \circ g the last machine to touch the number is ff, so the final answer is always something ff produced. In gfg \circ f the last machine is gg, so the final answer is always something gg produced. Since ff and gg do different jobs, 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 decides what kind of answer you get, so 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. First it must be a legal input for gg, because gg runs first. Then the number g(x)g(x) has to be a legal input for ff, because ff runs on that output. If either step fails, f(g(x))f(g(x)) never gets a value. Here is that reasoning stated exactly.

Which inputs a composition accepts#

To evaluate (fg)(x)=f(g(x))(f \circ g)(x) = f(g(x)) you carry out two steps in sequence, and the composite has a value exactly when both steps succeed.

The first step computes g(x)g(x). For this to produce a number at all, xx must be an allowed input of gg, that is, xx must lie in the domain of gg. If xx is not in the domain of gg, then g(x)g(x) does not exist, and there is nothing to hand to ff. In that case the composite f(g(x))f(g(x)) is undefined before ff even gets a turn.

The second step applies ff to the number g(x)g(x). 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. In other words, g(x)g(x) must lie in the domain of ff. If g(x)g(x) is a value that ff rejects, such as a number that would make ff divide by zero, then f(g(x))f(g(x)) is undefined again.

So f(g(x))f(g(x)) has a value if and only if both conditions hold at once. The first is that xx lies in the domain of gg, and the second is that g(x)g(x) lies in the domain of ff. Every input that fails either test is excluded from the domain of fgf \circ g, and every input that passes both is included. That two-part rule is not extra to memorize; it is just the two-step process read out loud.

The second half of that rule is the sneaky one, because it can exclude an input that looks perfectly innocent to the inner function.

Worked example 4 The domain of a composition

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

Substitute gg into ff to get the formula:

(fg)(x)=f(g(x))=f(x4)=1x4.(f \circ g)(x) = f(g(x)) = f(x - 4) = \frac{1}{x - 4}.

Now apply the two-part rule. The inner function g(x)=x4g(x) = x - 4 accepts every number, so the first test excludes nothing. The outer function f(x)=1xf(x) = \dfrac{1}{x} rejects only the input 00, so the second test demands g(x)0g(x) \neq 0:

x40    x4.x - 4 \neq 0 \;\Longrightarrow\; x \neq 4.

Watch what happens at x=4x = 4. The inner function is perfectly happy there, since g(4)=0g(4) = 0. But that output, 00, is exactly the one value ff cannot take, because f(0)=10f(0) = \dfrac{1}{0} is undefined. So 44 passes the first test and fails the second. Failing the second test throws 44 out of the domain, even though that input never looked dangerous in the rule for gg. A safe input like x=6x = 6 sails through: g(6)=2g(6) = 2 and then f(2)=12f(2) = \dfrac{1}{2}. The domain of fgf \circ g is all numbers except 44.

Check your understanding

Let f(x)=1xf(x) = \dfrac{1}{x} and g(x)=x7g(x) = x - 7. Which input is NOT in the domain of fgf \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.

Free Response Questions (FRQ)

Longer questions in parts, to be worked out on paper. Progressive hints, the answer on its own so you can check yourself and try again, then the full worked solution, plus a rubric to mark your own work against.

Free response Work it out on paper 5 questions 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. One of his puzzles was to find every rule that undoes itself. Run such a rule twice and you land back where you started.

You cannot even pose a question like that until you are willing to feed a function’s output into a function. That is the move this whole lesson rests on. A rule will take any input you hand it. That includes the answer another rule has just produced, 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. You have not done what ff then gg would have done.