Skip to content

Function Composition: Feeding One Output Into Another

Textbook

Given $g:A\to B$ and $f:B\to C$, their composition $f\circ g:A\to C$ is defined by $(f\circ g)(x):=f(g(x))$. The function on the right, $g$, acts first, followed by $f$. The first output must lie in the next function’s domain.

For $g(x)=x+1$ and $f(x)=x^2$, the results are $(f\circ g)(2)=9$ but $(g\circ f)(2)=5$. Composition is generally not commutative: changing the order changes the rule. The expression $f(g(x))$ denotes the composed function’s value, not a product of values.

Domain compatibility matters more than the formula’s appearance. For $g:A\to B$, every actual value $g(x)$ must lie in the domain of $f$; otherwise composition must be restricted to inputs where the next step is defined. Three compatible functions can be regrouped because both arrangements give $f(g(h(x)))$. This preserves the application order and does not permit exchanging the functions themselves.

Definitions and further reading: Mathematics for Computer Science.

In plain language

Composition resembles the order “socks, then shoes”: the first action supplies the input to the second. Exchanging the actions preserves their names but changes the result and the mood of nearby observers. Nested notation runs from the inside outward, so the outer step receives an intermediate result. That result must suit the next function; two working machines do not guarantee compatible parts.

Example

The original illustrative rules are $g(x)=x+1$, $f(x)=x^2$, and input 2. The example traces $f\circ g$ first, then calculates the opposite order.

Function Composition: Feeding One Output Into Another

Inner function

Adding 1 to input 2 gives 3.

Step 1 of 3: Inner function

Read all steps
  1. Inner function

    Adding 1 to input 2 gives 3.

    State
    2current3current
  2. Outer function

    Squaring the intermediate value 3 gives 9.

    State
    3current9current
  3. Opposite order

    Squaring 2 and then adding 1 gives the result 5.

    State
    2current4current5current

The calculation $f(g(2))$ has intermediate value 3, so squaring acts on three. In the reverse order, the intermediate value is 4 and one is added to it. Results 9 and 5 represent different composed functions rather than faulty arithmetic. The expanded rules are $(x+1)^2$ and $x^2+1$: parentheses show precisely which input the outer operation receives.

Related concepts: Functions.

The original text of this free article is published under CC BY-NC-ND 4.0.

Previous / next

Previous
Functions: One Output for Every Input
Next
Inverse Functions: Recovering Inputs From Outputs
  • Natural Numbers: Counting Discrete Objects

    Which values count discrete objects, and why the zero convention must be stated.

    Read article
    Math
  • Integers: Counting on Both Sides of Zero

    Positive and negative integers, zero, and the limits of integer division.

    Read article
    Math
  • Rational Numbers: Ratios of Integers

    Numbers as ratios of integers: equivalent fractions and the nonzero denominator requirement.

    Read article
    Math