Introduction to function composition
Function composition is concerned with combining two functions into one. For example, assume that we have two functions: f(x)
and g(x)
. The result of composition is the creation of a third function, let's call c, such that:
c(x) = f(g(x))
That is, the effect of calling c(x)
is the same as using the output of g(x)
as the input to the function f
. To illustrate this approach, let's use the following definitions:
f(x) = -x g(x) = 2*x c(5) = f(g(5)) = f(2*x) = -(2*5) = -10
The effect is that the g(x)
function is called first. Its results are then used as input to the f(x)
function. This capability allows more complex functions to be created in a more flexible and useful manner.