Composition of Functions

We are familiar with the concept of function. It is a relation that makes a unique image as output for a given input. A function takes some input and spits out some output based on a rule. The example of a function is f(x) = 1/x. In this function, the rule is to take the input ‘x’ and output its reciprocal. Note that f(x) = 1/x is not defined at x = 0.

It is also known that not all relations qualify as a function. For example, y2 = 2x is a relation that does not qualify as a function since for a given input, say x = 2, the equation outputs two values of y i.e., +2 and -2.

The idea of composition of functions

Let us try to make an analogy that makes it easy to understand the concept of composition of functions. In this analogy, a function is treated as a machine. Consider a machine ‘A’ whose purpose is to put mozzarella cheese, parmesan cheese, garlic, mushroom and other delicious toppings on a pizza dough placed on a conveyor belt passing through it. There is another machine ‘B’ whose sole purpose is to maintain a constant cooking-temperature inside it. Now imagine that we have a fresh flattened pizza dough. You can realize that if this dough passes through the machine ‘A’, it will receive pizza toppings over it and the output will be an uncooked pizza filled with toppings. On the other hand, if the dough drifts through the machine ‘B’, it will get cooked and the output will be a brownish looking plane piece of bread.

What if we put machine ‘B’ in cascade with the machine ‘A’? That is right, we will get a composition of A and B that makes delicious pizzas. We input the flattened pizza dough at one end and obtain an appetizing ready-to-eat pizza at the other end. In this case, the output of the machine ‘A’ will be an input for the machine ‘B’

Imagine what would be the product if the machine ‘B’ is placed first and then machine ‘A’ is placed. Does the order even matter? You may realize what the product will look like and you would not like to call it a legit pizza.

A function can be represented as say, f(x)=3x+1, where x is called the independent variable and represents the input to the function. This equation is read as “f of x is equal to 3x+1”. What does this actually mean? The relation says that the function f(x) takes an input number and generates a corresponding output number. The output number is none other than one added to thrice the value of the input number. For example, if x=2, the function evaluates to three times 2 plus one i.e., 7. If x=1, the function evaluates to three times 1 plus one i.e., 4. In essence, 7 and 4 are images of 2 and 1 (respectively) as “seen through the lens of the function f(x)”. Is it possible that we place another lens (another function) in front of the output of f(x)? What will happen if we do so? It can be realized that the second function, say g(x), will cast an image of the output of f(x). In other words, f(x) will act as an input to g(x). As, the variable x represents the input of g(x), therefore, the above statement can be written in condensed form as g(f(x)), read as “g of f of x”. Note that we could have also formed a different arrangement f(g(x)). This is the gist of composition of functions. It can also be noted that we can put as many lenses in any particular arrangement as we like i.e., the composition can be of more than two functions.

It is not difficult to understand that the composition of functions also represents another function (domain has to be taken care of). Think about it!


The composition of two functions is the representation where one function acts as input to another function. The function whose output acts as input to another function is called the inner function and the other one is called the outer function. Two functions can be written in two different composite forms. It is worth mentioning that the idea of composition can be extended to more than two functions.


Given two functions f(x) and g(x), they can be represented in two different composite forms as

(f o g)(x) = f(g(x))                                    (1)

(g o f)(x) = g(f(x))                                    (2)

The first expression means that first g(x) is operated on x, then f(x) is operated on g(x) as f(g(x)). Similarly, the second expression means that first f(x) is operated on x, then g(x) is operated on f(x) as g(f(x)). Let us visit an example to understand the process. Consider two functions f(x)=4\sqrt{x} and g(x)=2x-10. The composition f(g(x))=4\sqrt{g(x)} which is quite intuitive. Note that we have just replaced x with g(x) in the expression of f(x). The next step is to substitute g(x) in form of x. This gives, f(g(x))=4\sqrt{2x-10}. Following the same procedure, we first find the g(f(x)) = 2f(x)-10. After replacing the expression of f(x), we get g(f(x)) = 2(4\sqrt{x})-10=8\sqrt{x}-10. Interesting!

It can be observed that f(g(x)) and g(f(x)) are not equal at all. Can you see why? It is indeed true in general that f(g(x)) ≠ g(f(x)). As a fun exercise, try to come up with an example where f(g(x)) is equal to g(f(x))?

Exercise: As an exercise, find the two different compositions i.e., h(g(x)) and g(h(x)) of two functions h(x) = 7x2 + 2x and g(x) = x-1/2. It should be noted that the x in h(x) appears two times. Both of them should be replaced by g(x) while finding h(g(x)).

What if one of the functions or both of them is/are constant functions? Let us see what happens. Consider, two functions g(x)=2x+\sqrt{x} and k(x)=4. The composite function g(k(x)) can be found by replacing x by k(x) in the expression of g(x). This would yield, g(k(x))=2(k(x))+\sqrt{k(x)}. Substitute the expression of k(x) and we get g(k(x))=2(4)+\sqrt{4}=8+2=10. Can you guess what would be k(g(x))? It is a bit tricky. By following the original procedure, we simply get k(g(x)) = 4 (there is no x in the constant function k(x) to replace g(x) with!).

Self-composition of a function

Is it possible to obtain a composition where both the functions are the same i.e., a composition f(f(x))? Why not? This is called self-composition of a function. It is represented as:

(f o f)(x) = f(f(x))                                   (3)

It is not surprising to note that this process can be extended to involve three f(x)’s or more.

Let us try some examples. Given f(x)=\sqrt{x}, find its self-composition. Looks complicated? Do not worry, just follow the steps that we have learnt above (i.e., procedure of finding the composition of two functions). The self-composition of f(x) can be written as (f o f)(x)=f(f(x))=\sqrt{f(x)}, substituting the relation of f(x), we get f(f(x))=\sqrt{\sqrt{x}}.

As the second example, given that g(x) = 5x-1, we can find its self-composition as (g o g)(x) = g(g(x)) = 5g(x) – 1 = 5(5x – 1) – 1 = 25x – 6.

Exercise: Find respective self-compositions of the following functions.

  • g(x) = 4/x2
  • f(x) = 45
  • h(x) = 3x+1

A special composition

The concept of composition leads to a unique case. There exists a special composition of two functions f(x) and g(x) such that,

f(g(x)) = g(f(x)) = x                                  (4)

This is in fact connected to the idea of inverse of a function. The inverse of a function f(x) is a function for which the above expression holds. In the above case, g(x) is inverse function of f(x). Alternatively, f(x) is the inverse function of g(x). The topic of inverse function is covered in another article in great detail.

Domain of Composition of function

You may recall that the domain of a function is the set of all the values of x/independent variable/input for which the function is defined. For example, the domain of the function f(x) = x2 is the set of all Real numbers. The domain of the function f(x)=\sqrt{x} is the set of positive real numbers only, since the square root of negative numbers does not exist on real line.

It is known that composition also involves functions. Therefore, a question arises that what would be the domain of the composition of functions. To explore why this question is so important, consider the two functions f(x)=2x^{2}+1 and g(x)=\sqrt{x}. The composition f(g(x)) can be evaluated as f(g(x))=2(g(x))^{2}+1=2x+1. It can be argued that “2x+1” is defined for all values of x therefore, the domain of the composition f(g(x)) should be the set of real numbers. Right? But this is where it gets complicated. You see that f(g(x)) has g(x) as the inner function. The function g(x) is not defined for negative real numbers, therefore, the domain of f(g(x)) is positive real numbers only since the g(x) is there to restrict the overall domain even though f(x) is defined for all real numbers.

Note that the outer function of a composition can also impose a restriction on the overall domain of the composition of functions.

Why the restriction?

If we consider the last example, it is rational to debate over the fact that since “2x+1” is defined for all values of x, the domain of the composition should be the set of all real numbers. This issue can be resolved if we look back at the concept of composition of functions. You may recall that in a composition, say f(g(x)), the input is first passed to the function g(x) and its output is passed to f(x). The function g(x) can only accept the input values for which it is defined i.e., it can only accept values that are part of its domain. In the example, the domain of g(x) is set of positive real numbers. In other words, as the function g(x) is confronted first by a number when that number is passed into the “composition machine”, therefore, the domain of the composition is restricted already by g(x).

It is further observed that in this example, a separate composition g(f(x))=\sqrt{f(x)}=\sqrt{2x^{2}+1} can be defined. Let us see what is its domain. It can be observed that f(x) is defined for all real numbers. The range of f(x) is all positive real numbers. Therefore, the domain of g(f(x)) is the set of all real numbers. It can be thought of as the function f(x) converts even the negative numbers to positive at its output and thus, g(x) receives positive numbers even the value of x is negative. This provides an important insight that, in general, domain of f(g(x)) is not equal to domain of g(f(x)).

The domain of the composition f(g(x)) may further be restricted if f(x) is not defined for some values in range of g(x) (since output/range of g(x) is input/domain of f(x) for the composition f(g(x))). For example, if f(x)=\sqrt{x} and g(x)=x^{1/3}, it is evident that g(x) exists for all real numbers (negative and positive) i.e., the domain of g(x) is the set of all real numbers. Unfortunately, f(x) is not happy about it, because f(x) cannot accept negative values from g(x) (as for say, x=-8, g(x)=-2). Therefore, the domain of overall composition is restricted as f(x) is not comfortable with whole range (set of output values) of g(x). In this case, the domain of the composition f(g(x)) becomes set of all positive real numbers only. Note that domain of the composition g(f(x)) is also set of positive real numbers.

Exercise: Given the functions f(x) = 2x and g(x) = x2, find the domain of the compositions f(g(x)), g(f(x)), f(f(x)) and g(g(x)).

Is composition of functions a function also?

So far, we have not discussed about the nature of the composition of functions i.e., whether a composition qualify as function itself or not, or whether it depends on the individual functions. The fact is that the composition of a functions is also a function. It is not difficult to prove how. Recall that it is the characteristic property of a function that it makes a single image of a given input. Thus, if two functions are put in cascade (back-to-back), then the first function will make a single image of the original input x. The second function will take the image as input and output a single value. In other words, we input a number into the composition of functions and get the same number always (i.e., for a given input there is only one output). Does this sound like a function now? Yes, you are right. This is the very characteristic of a function. Hence, the composition of functions is also a function itself. Note that the domain and the range have to be decided based on the nature and order of the functions (as discussed above). The composition of functions, in fact, has a name. It is called a composite function. The composite functions can be represented as:

h(x) = (f o g)(x) = f(g(x))                                 (5)

k(x) = (g o f)(x) = g(f(x))                                 (6)

Exercise: For the exercises given above, verify that the composition of functions qualify the definition of functions. Also, find the range of those composite functions.

Decomposing a function as composition of other functions

Prepare to get amazed now. We have proved by now that the composition of two functions is also a function itself, called as the composite function. It has also been observed that there is nothing unique about the mathematical expressions of the composite functions. They just look like regular expressions. So, here is the statement that you may find fascinating. “A function can be decomposed into more than one functions OR A function can be represented as a composition of more than one functions”.

As a matter of fact, it is true that a function can be decomposed into two individual functions with their independent domains and ranges. It should be kept in mind that the decomposition is not unique, however. A function can be decomposed into different kinds of functions. Let us try to decompose a function into two individual functions in composite form. Consider a very simple function h(x) = 2x-3. The term 2x can be replaced by say, f(x). This will reduce the equation into h(x) = g(f(x)) = f(x)-3. Replace f(x) by x. This gives us another function g(x) = x-3.  Thus, we have successfully decomposed a function h(x) into two individual functions g(x) and f(x) as g(f(x)). Always remember that order matters. The function f(g(x)) will be some different function (in general).

Exercise: Decompose the following functions into two individual functions each. Verify that the relations you obtain are functions or not. Try to find the domain of those functions.

  • f(x) = x2
  • f(x) = 1/x + 5

How to find composition of more than two functions?

It is stated in this article that composition can be of more than two functions. But how is it possible to evaluate such a complicated structure. Fortunately, it is way easier than you think. As we have discussed that the composition of a function is another function, therefore, we can reduce the nested composite structure one step at a time. For example, if a nested structure contains five functions f(g(h(k(l(x))))), we can separately evaluate composition of two innermost functions as m(x) and substitute it as f(g(h(m(x)))). We will repeat the procedure until only a single function remains. No matter how many functions you nest, the ultimate composition will be a function (we have proved already).