What is Composition?
You have two functions. You chain them — the output of the first becomes the input to the second.
f:A→B,g:B→C
Composition: g∘f
This reads: “g of f” or “g after f”
How It Works
(g∘f)(x)=g(f(x))
First apply f, then apply g to the result.
Example
f(x)=x+1 g(x)=x2
Find (g∘f)(2):
- First: f(2)=2+1=3
- Then: g(3)=32=9
(g∘f)(2)=9
Order Matters
g∘f=f∘g
Same functions, different order:
(g∘f)(2)=g(f(2))=g(3)=9
(f∘g)(2)=f(g(2))=f(4)=5
Different results.
Always apply the inner function first.
Domain Requirements
For g∘f to exist:
- The codomain of f must match the domain of g
- Otherwise, outputs of f can’t be inputs to g
f:A→B,g:B→C,g∘f:A→C
Properties
Associativity:
(h∘g)∘f=h∘(g∘f)
Grouping doesn’t matter when composing three functions.
Identity function:
idA(x)=x
f∘idA=f idB∘f=f
Composing with identity changes nothing.