# Function

A function f is a rule for taking one item x, as an input, and producing another item f(x) as an output. Frequently this will involve numbers, so for example the polynomial f(x) = x^2 takes numbers as inputs, and outputs their squares. So in this case, if 1 is input, 1 is output, whilst if 2 is input, 4 is output. However, there are functions that take other objects as inputs. For example, rotation can be thought of as a function that takes a shape as an input, and outputs that shape rotated by a certain number of degrees.

Usually, a function will be written with a formula for the output, in terms of the input. For example, in the case of f above, the formula is f(x) = x^2. When we want to input a particular value, we insert in in the place of x. So f(1) = 1^2 = 1*1 = 1.

## Examples

If the inputs and outputs are numbers, then the most obvious examples are the polynomials. f(x) = x + 1 is a simple example, a function that adds one to the input. f(1) = 1 + 1 = 2, f(2) = 2 + 1 = 3, and so on. Also, g(x) = 2x is a function that doubles the input. g(3) = 6, g(4) = 8. Another example is h(x) = 2x + 1. This doubles the output, and adds 1. The result is that h(1) = 2*1 + 1 = 3, h(2) = 2*2 + 1 = 5.

i(x) = x is another function that does nothing to x. So i(1) = 1, i(2) = 2, and so on. This concept will be important later on.

Some functions do not have numbers as outputs. For example, we can define a function pres that takes a number n, and returns the name of the nth American President. So, pres(1) = George Washington (the first president), pres(2) = John Adams (the second president), and so on.

Similarly, not all functions take numbers as inputs. For example, we can define a function pres2 that takes the name of an American President (the nth in sequence), and outputs n. Thus pres2(George Washington) = 1, pres2(John Adams) = 2. This is the reverse of pres defined above.

## Composition

We can take 2 functions, and combine them to form another function, by applying them one after another. This is called composition. For example, take f(x) = x + 1, and g(x) = 2x. We can apply g to x, and then f to the output g(x), to get f o g, which is the function we get by combining the functions f and g in this way. We can think of this as like a factory production line, where the output of one process or machine becomes the input of the next. Here, the output of g becomes the input of the function f.

In our example, (f o g)(x) = f(g(x)) = f(2x) = 2x + 1. So the combined function f o g is in fact h, which we met earlier. To test this, we can take (f o g) applied to 8. g(8) = 2*8 = 16. Then we apply f to 16, to get f(16) = 16 + 1 = 17. 17 = 2*8 + 1, as expected.

We can also apply the functions in the opposite order to get g o f, where we apply f first then g. f(x) = x + 1, and g(x+1) = 2(x+1) = 2x + 2. So (g o f)(x) = 2x + 2. So, the order in which we combine the functions makes a difference to the final result. Again, we test this formula by applying the function to 8. f(8) = 9, and g(9) 18. 18 = 2*8 + 2, as expected.