# Function composition

In mathematics, function composition is a way to make a new function from two other functions.

If we let f be a function from X to Y and g be a function from Y to Z then we say that g composed with f is written as g ∘ f a function from X to Z (notice how it is usually written in the opposite way to how people would it expect it to be as we will explain below).

The value of f given the input x is written as f(x). The value of g ∘ f given the input x is written (g ∘ f)(x) and is defined as g(f(x)) (which means our way of writing g composed with f makes sense).

Here is another example. Let f be a function which doubles a number (multiplies it by 2) and let g be a function which subtracts 1 from a number.

These would be written as:

${\displaystyle f(x)=2x}$
${\displaystyle g(x)=x-1}$

g composed with f would be the function which doubles a number and then subtracts 1 from it:

${\displaystyle (g\circ f)(x)=2x-1}$

f composed with g would be the function which subtracts 1 from a number and then doubles it:

${\displaystyle (f\circ g)(x)=2(x-1)}$

## Properties

Function composition can be proven to be associative,[1] which means:

${\displaystyle f\circ (g\circ h)=(f\circ g)\circ h}$

Function composition is in general not commutative however,[2] which means:

${\displaystyle f\circ g\neq g\circ f}$

This can be seen in the first example where (g ∘ f)(2) = 2*2 - 1 = 3 and (f ∘ g)(2) = 2*(2-1) = 2.