Function composition is indeed creating a new function that applies one function on the output of another one. More generally, composition allows to create a new function by combining several other functions.
Pure functions are functions that always provide the same output for the same input and have no side effects. So a function without any surprise, that you can call how often you may need to since only its result matters.
You can compose pure functions. You will get, by definition, a pure function.
You can also compose impure functions. It's just that it's more risky.
Take an example with h(x) = g(f(x)):
- If f and g have two totally independent side effects, sf and sg, these side effects will happen in the following order: first sf, then sg.
- If the side effects are not independent, then composition can have unexpected effects.
Suppose now that you have a more complex composition, using several variables and more than two functions: h(x,y) = g(f1(x,y),f2(x,y)). As already said, if all these functions are pure the result will be pure without surprise. If these functions are impure a lot of open questions arise:
- Suppose each of these function has independent side effects sg, sf1, sf2. Here you no longer can predict the order of these side effects: it could be sf1, sf2, sg. It could as well be sf2 sf1 sg or even sf1 and sf2 exactly in the same time and then sg.
- Suppose that these side effects are not independent, for example that sf1 adds 1 to a global variable and sf2 multiplies by 2 the same global variables, the effect of h on the global variable would be unpredictable.