# Function Composition

In math, function composition is a way of combining 2 functions into one by "plugging" the output of one function into the input of another function.

## Example

Let's first look at an example of this. Let's say we have two functions: `f` and `g` defined as follows:

$f\left(x\right):=2x$ $g\left(x\right):=x+5$

Then we can combine them using function composition as follows:

$\left(f\circ g\right)\left(x\right):=f\left(g\left(x\right)\right)=2\left(g\left(x\right)\right)=2\left(x+5\right)=2x+10$

## Function Composition Operator

Note that we have introduced this new operator that combines functions in this manner:

$f\circ g$

Speaking, we'd say this as: "`f` circle `g`" or alternatively "`f` of `g`".

## Not Commutative

Note that the order that you write that matters. In other words...

$f\circ g$

... is NOT the same as...

$g\circ f$

To illustrate this, if we use the same `f` and `g` from our previous example, then we can see that:

$\left(g\circ f\right)\left(x\right):=g\left(f\left(x\right)\right)=\left(f\left(x\right)\right)+5=\left(2x\right)+5=2x+5$

Thus we can clearly see that, in general:

$f\circ g\ne g\circ f$

(Although there will be some special cases where they will actually be equal.)

## Another Example

Let's look at another example. Let's says we have two (new) functions (that have absolutely nothing to do with the functions in our previous example): `f` and `g` defined as follows:

$f\left(x\right):=8x+4{x}^{2}$ $g\left(x\right):=x-2$

Then our function composition looks like:

$\left(f\circ g\right)\left(x\right):=f\left(g\left(x\right)\right)=8\left(g\left(x\right)\right)+4{\left(g\left(x\right)\right)}^{2}=8\left(x-2\right)+4{\left(x-2\right)}^{2}=4{x}^{2}-8x$

## Associative

We can combine more than two functions at a time. For example, if we had 3 functions, `f`, `g` and `h`, we could write:

$f\circ g\circ h$

The astute reader may have noticed that I have written this in a way that implies that function composition is associative. And in fact it is. In other words:

$f\circ \left(g\circ h\right)=\left(f\circ g\right)\circ h$

And when we have this property, we tend to just drop the brackets and write:

$f\circ g\circ h$

Or, in other words:

$f\circ \left(g\circ h\right)=\left(f\circ g\right)\circ h=f\circ g\circ h$

## Example With 3 Functions

Here's an example with 3 functions. Let's say we have 3 functions, `f`, `g` and `h`, defined as follows:

$f\left(x\right):=1+x$ $g\left(x\right):=5+20x$ $h\left(x\right):=7x$

Then we can combine the 3 functions we with following function composition and get:

$\left(f\circ g\circ h\right)\left(x\right):=f\left(g\left(h\left(x\right)\right)\right)=1+\left(g\left(h\left(x\right)\right)\right)=1+\left(5+20\left(h\left(x\right)\right)\right)=1+\left(5+20\left(7x\right)\right)=6+140x$

## Inputs and Outputs

So far, I've picked the example very carefully. I wanted to keep thigs simple so we could focus on learning the topic. But now that we've learned it, we need to look at some things we need to be careful and mindful of. Let's look at this function composition:

$f\circ g$

To actually be able to do this, we need the input of `f` to match the output of `g`.

To illustate why this is important, let's pick two functions which we cannot compose, because their inputs and outputs are incompatible.

$f\left(x,y\right):=3x+y$ $g\left(x\right):=x+5$

In this case, $f\circ g$ does not make any sense since the input of `f` is 2 dimensional and the output of `g` is one dimensional.

-- Mirza Charles Iliya Krempeaux