Let f(x)=x+1 and g(x)=x2. We can define a new function h as
h(x)=f(g(x))=f(x2)=x2+1. That is, to obtain h(x), we substitute g(x) for x in the expression f(x).

To generalize this process suppose f and g are any two given functions. We start with a number x in the domain of g and apply g to it to get g(x), then we apply f to g(x) and thereby obtain the number f(g(x)). Obviously this process presupposes that it makes sense to calculate f at the point g(x). In other words, the new function is defined only if g(x) is in the domain of f (otherwise we cannot use g(x) as the input for f).

Consecutive application of functions is known as composition of functions. The new function that takes x and assigns to it the value f(g(x)) is often denoted by fg. The symbol fg is read “f circle g.” Figure 1(a) shows the composition fg as a machine diagram and Figure 1(b) illustrates it as an arrow diagram.

(a)
(b)
 Fig 1(a) Machine digram for fg. A composite (or composition) function fg applies the g machine to the input x and then uses the output g(x) as the input for the f machine.
Fig. 1(b)  Arrow diagram for fg. If x is in the domain of g and g(x) is in the domain of f, then we may compose f and g to form fg.
Let f and g be two functions. For those points x in the domain of g for which g(x) lies in the domain of f, the composite function fg is defined by
(fg)(x)=f(g(x)).
  • In set notation, the domain of fg is
    Dom(fg)={x| xDom(g) and g(x)Dom(f)}.
  • Note that the order of composition of two functions matters. In calculating (fg)(x), we first evaluate g at x and then use g(x) as the input to calculate f of the result. But in calculating (gf)(x), we first evaluate f at x and then calculate g at the the point f(x). Therefore, (fg)(x) is often quite different from (gf)(x).
Example
Let f(x)=x and g(x)=1+x2. Find the following functions and their domains.
(a) fg
(b) gf
Solution
We have
Dom(f)={x| x0}=[0,),Dom(g)=R. (a)
(fg)(x)=f(g(x))=f(1+x2)=1+x2.
Because g(x)=1+x21 for all x, g(x) is always in the domain of f. Therefore
Dom(fg)=R. (b) 
(gf)(x)=g(f(x))=g(x)=1+(x)2=1+x\footnotesize( if x0)
Because the domain of g is the entire set of real numbers, f(x) always lies in the domain of g. However, f is defined only for x0. Therefore, the domain of gf is the same as the domain of f, namely
Dom(gf)=Dom(f)={x| x0}=[0,).
Example
If f(x)=x+1x1, find the function ff and its domain.
Solution
Let’s first find ff
(ff)(x)=f(f(x))=f(x+1x1)=x+1x1+1x+1x11=x+1+x1x1x+1(x1)x1=2x2=x
Just by looking at the above formula one may be tempted to say that the domain of ff is R. However, the domain of ff consists of all x that 
(1) x lies in the domain of f,
(2) f(x) lies in the domain of f

The domain of f is all real numbers except x=1 (recall that division by zero is not defined). So to satisfy the first condition we have to exclude x=1. To satisfy the second condition, we also have to exclude x for which
x+1x1=1. It turns out that this equation has no solution because it is equivalent to x+1=x1 or 1=1. This means that f(x) is always in the domain of f, and the second conditions is automatically satisfied without any further restriction on x. Thus in this specific example
Dom(ff)=Dom(f)={x| x1}=R{1}.

Example
Find functions f and g such that h=fg given that
h(x)=cos(x2).
Solution
To calculate h(x) we first square x and then take the cosine of x2. We can therefore set
Misplaced & and
Misplaced & We observe that
(fg)(x)=f(g(x))=f(x2)=cos(x2).
We can combine three or more functions by composing them two at a time. For example, the function F given by
F(x)=12+x2, is a composition of three functions F=fgh, where
h(x)=x2,g(x)=x+2,f(x)=1x. Notice that we can compose f and g first then compose fg with h or we can first compose g and h and then compose f and gh. In general, composition is associative. That is
fgh=(fg)h=f(gh)
Example
Find three functions f,g, and h such that F=fgh given that
F(x)=|x|+3.
Solution
To calculate F(x), we fist take the absolute value of x, add 3, and then take the square root. We can therefore set
h(x)=|x|, g(x)=x+3, and
f(x)=x. With this choice of f,g, and h, we observe that
(fgh)(x)=f(g(h(x)))=f(g(|x|))=f(|x|+3)=|x|+3.