Skip to content

Composition and Inverse Functions: When an Inverse Exists and How to Find It

How to compose functions, why order matters, when a function has an inverse, and how to find and check one, with examples on finite sets and the integers.

By Published 4 min read

Reign Creative LLC makes Discrete Math: Study & Quiz, which this article links to, so we have a commercial interest in it. How we write and check articles.

AI-assisted article. Drafted with AI tools from the sources cited below and checked by automated tests and an AI editorial review. No person has reviewed it yet. How we write and check articles.

Key takeaways

  • (g ∘ f)(x) = g(f(x)): apply f first, then g, and the composition is only defined when f's outputs lie in g's domain.
  • Composition is associative but not commutative: h ∘ (g ∘ f) = (h ∘ g) ∘ f, while g ∘ f and f ∘ g usually differ.
  • A function has an inverse exactly when it is a bijection, and the inverse undoes it: f⁻¹(f(x)) = x and f(f⁻¹(y)) = y.
  • To find an inverse, solve y = f(x) for x, then check both compositions give the identity.
  • The inverse of a composition reverses the order: (g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹.

Functions become much more useful once you can chain them together and, sometimes, run them backwards. Composition is the chaining; the inverse is the running backwards. Both are short definitions with a few traps, mostly to do with order.

Composition: one function after another

Given f: A → B and g: B → C, the composition g ∘ f: A → C is defined by

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

Read it "g after f" or "g of f of x". The function written on the right acts first. Applied Discrete Structures defines it with exactly these domains and codomains: the outputs of f must land in the set where g is defined, or the composition does not exist.

Example on a finite set. Let A = {1, 2, 3}, with f(1) = 2, f(2) = 3, f(3) = 3 and g(1) = 1, g(2) = 1, g(3) = 2.

x f(x) g(f(x)) g(x) f(g(x))
1 2 1 1 2
2 3 2 1 2
3 3 2 2 3

Compare the third and fifth columns: g ∘ f and f ∘ g are different functions.

Example on the integers. With f(n) = n + 1 and g(n) = 2n:

  • (g ∘ f)(n) = g(n + 1) = 2n + 2
  • (f ∘ g)(n) = f(2n) = 2n + 1

Composition is not commutative. Order matters, and swapping it changes the answer.

What composition does preserve

Composition is associative: for any f, g and h with compatible domains, h ∘ (g ∘ f) = (h ∘ g) ∘ f, because both sides send x to h(g(f(x))). So a chain like h ∘ g ∘ f needs no brackets.

Applied Discrete Structures states two more facts, leaving their proofs as exercises; both are worth being able to reproduce:

  • The composition of injections is an injection. If g(f(a)) = g(f(b)), then f(a) = f(b) because g is one-to-one, and a = b because f is one-to-one.
  • The composition of surjections is a surjection. For any c ∈ C, pick b with g(b) = c, then a with f(a) = b; then g(f(a)) = c.

Together these show the composition of two bijections is a bijection. The definitions behind them are in injective, surjective and bijective functions.

The identity function

For any set A, the identity function i_A(x) = x leaves everything where it is. It plays the role in composition that 1 plays in multiplication: f ∘ i_A = f and i_B ∘ f = f for any f: A → B.

Inverses: running a function backwards

A function g: B → A is the inverse of f: A → B if

g(f(x)) = x for every x ∈ A, and f(g(y)) = y for every y ∈ B.

In other words, g ∘ f = i_A and f ∘ g = i_B. The inverse is written f⁻¹. Note that f⁻¹ means the inverse function, not 1/f.

When does an inverse exist? Exactly when f is a bijection.

  • If f were not injective, two inputs a ≠ b would share an output y, and f⁻¹(y) would have to equal both.
  • If f were not surjective, some y would have no input, and f⁻¹(y) would have nothing to be.

MathWorld defines a bijection as a transformation that is both one-to-one and onto, which is precisely the condition for "undo" to be a function.

Finding an inverse

On a finite set, reverse every arrow. Applied Discrete Structures uses this example: on A = {1, 2, 3}, f(1) = 2, f(2) = 3, f(3) = 1. Reversing the arrows gives f⁻¹(2) = 1, f⁻¹(3) = 2, f⁻¹(1) = 3.

With a formula, write y = f(x) and solve for x.

Example. f: ℝ → ℝ, f(x) = 5x − 3. From y = 5x − 3, x = (y + 3)/5, so f⁻¹(y) = (y + 3)/5. Check both directions:

  • f⁻¹(f(x)) = ((5x − 3) + 3)/5 = x
  • f(f⁻¹(y)) = 5 · (y + 3)/5 − 3 = y

The check matters because solving for x can hide a domain problem. The same formula on the integers, f: ℤ → ℤ, is injective but not surjective (y = 0 would need x = 3/5), so it has no inverse there.

Restricting the domain. g(x) = x² is not a bijection on ℝ, but restricted to [0, ∞) → [0, ∞) it is, with inverse √y. Likewise eˣ: ℝ → (0, ∞) has inverse ln y.

Inverses in modular arithmetic. On the remainders {0, 1, …, 6} under multiplication mod 7, the function x ↦ 3x is a bijection, and its inverse is x ↦ 5x, because 3 · 5 = 15 ≡ 1. Finding such inverses systematically is the subject of modular inverses.

The inverse of a composition

If f and g are both bijections, then

(g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹.

To undo "f, then g", undo g first, then undo f. The familiar analogy is getting dressed: socks then shoes, so to undress, shoes then socks. A one-line check: (f⁻¹ ∘ g⁻¹) ∘ (g ∘ f) = f⁻¹ ∘ (g⁻¹ ∘ g) ∘ f = f⁻¹ ∘ f = i, using associativity.

Four errors with composition and inverses

  1. Applying the left function first. In g ∘ f, f acts first.
  2. Assuming every function has an inverse. Check it is a bijection between the stated domain and codomain.
  3. Checking only one composition. On infinite sets especially, g ∘ f = i alone does not make g the inverse; you need f ∘ g = i as well.
  4. Writing (g ∘ f)⁻¹ = g⁻¹ ∘ f⁻¹. The order reverses.

Levin's Discrete Mathematics: An Open Introduction is a good companion for the underlying definitions of domain, codomain and range. Functions belong to the sets, functions and countability unit of Discrete Math: Study & Quiz, whose lessons include knowledge checks; it is listed with our education apps, and the discrete math hub links the neighbouring topics.

Good to know

Frequently asked questions

What does g ∘ f mean?
The composition 'g after f': (g ∘ f)(x) = g(f(x)). You apply f to x first, then apply g to the result. It requires every output of f to be a valid input of g.
Is function composition commutative?
No. With f(x) = x + 1 and g(x) = 2x, g(f(x)) = 2x + 2 but f(g(x)) = 2x + 1. Composition is associative, though, so a chain of three or more functions can be grouped either way without changing the result.
When does a function have an inverse?
When it is a bijection: one-to-one, so each output comes from a single input, and onto, so every element of the codomain has an input to return to. If either property fails, there is no function that undoes it on the whole codomain.
What is the inverse of g ∘ f?
f⁻¹ ∘ g⁻¹, provided f and g are both bijections. Undoing 'f then g' means undoing g first and then f, in the same way that taking off shoes and socks reverses the order you put them on.

Sources

  1. Function Composition (Applied Discrete Structures) (opens in a new tab)discretemath.org — accessed
  2. Functions (Discrete Mathematics: An Open Introduction, 3rd edition, Oscar Levin) (opens in a new tab)Open Math Books — accessed
  3. Bijection (opens in a new tab)Wolfram MathWorld — accessed

About the author

— The independent app studio that builds the apps covered on this site and publishes every article here. On Google Play it publishes as Reign Collective Apps.

We use AI tools to help research, draft and edit our articles. Each article lists the sources its factual claims rely on, and details about our apps come from their live Google Play listings. No article on this site has been reviewed by an independent expert unless the reviewer is named on it.

No person at Reign Creative LLC has read this article in full. It was published after automated checks and an AI editorial review, which are described in our editorial policy and are not the same as a person reading every sentence.

Spotted an error? Tell us — how corrections are handled is set out in our editorial policy.