Course contentsShow
← Computer Science
Lesson 80 of 2,8722. Discrete Mathematics for CSFree lesson

Composition of Functions

Learning how to combine functions through composition and understanding associativity of composed functions.

Composition of Functions

What you'll learn: How to chain two functions together so the output of one becomes the input of another, and why the order matters but grouping doesn't.

What is Function Composition?

Function composition is like a pipeline: you take two functions and connect them so the output of the first becomes the input of the second. If you have a function f that takes some input and produces an output, and another function g that can accept that output, you can compose them.

We write this as (g ∘ f)(x), which means "apply f to x first, then apply g to that result." In other words: g(f(x)).

Order Matters!

Think of it like getting dressed: putting on socks then shoes is very different from putting on shoes then socks! Similarly, (g ∘ f) typically gives a different result than (f ∘ g).

Example: Let f(x) = x + 2 and g(x) = x × 3

  • (g ∘ f)(5) = g(f(5)) = g(7) = 21
  • (f ∘ g)(5) = f(g(5)) = f(15) = 17

Different results!

Associativity: Grouping Doesn't Matter

Here's the good news: when composing three functions, you can group them however you want. (h ∘ (g ∘ f)) gives the same result as ((h ∘ g) ∘ f). You always apply them right-to-left, so the grouping parentheses don't change the outcome—just like addition where (2 + 3) + 4 = 2 + (3 + 4).

Key Takeaway: Function composition creates a new function by chaining outputs to inputs; the order of composition matters critically, but how you group multiple compositions doesn't change the result (associativity holds).