# 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:

Then we can combine them using *function composition* as follows:

## 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...

To illustrate this, if we use the same `f`

and `g`

from our previous example, then we can see that:

Thus we can clearly see that, **in general**:

(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:

Then our *function composition* looks like:

## 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:

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:

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:

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

## 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.

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.