section_path: General properties/Function composition
section_name: Function composition
paragraph_text: Function composition
  
  Given two functions f: X → Y {\displaystyle f:X\to Y} and g: Y → Z {\displaystyle g:Y\to Z} such that the domain of g is the codomain of f, their composition is the function g ∘ f: X → Z {\displaystyle g\circ f:X\rightarrow Z} defined by
  
  That is, the value of g ∘ f {\displaystyle g\circ f} is obtained by first applying f to x to obtain y = f (x) and then applying g to the result y to obtain g (y) = g (f (x)). In this notation, the function that is applied first is always written on the right.
  
  The composition g ∘ f {\displaystyle g\circ f} is an operation on functions that is defined only if the codomain of the first function is the domain of the second one. Even when both g ∘ f {\displaystyle g\circ f} and f ∘ g {\displaystyle f\circ g} satisfy these conditions, the composition is not necessarily commutative, that is, the functions g ∘ f {\displaystyle g\circ f} and f ∘ g {\displaystyle f\circ g} need not be equal, but may deliver different values for the same argument. For example, let f (x) = x and g (x) = x + 1, then g (f (x)) = x 2 + 1 {\displaystyle g(f(x))=x^{2}+1} and f (g (x)) = (x + 1) 2 {\displaystyle f(g(x))=(x+1)^{2}} agree just for x = 0. {\displaystyle x=0.}
  
  The function composition is associative in the sense that, if one of (h ∘ g) ∘ f {\displaystyle (h\circ g)\circ f} and h ∘ (g ∘ f) {\displaystyle h\circ (g\circ f)} is defined, then the other is also defined, and they are equal, that is, (h ∘ g) ∘ f = h ∘ (g ∘ f). {\displaystyle (h\circ g)\circ f=h\circ (g\circ f).} Therefore, it is usual to just write h ∘ g ∘ f. {\displaystyle h\circ g\circ f.}
  
  The identity functions id X {\displaystyle \operatorname {id} _{X}} and id Y {\displaystyle \operatorname {id} _{Y}} are respectively a right identity and a left identity for functions from X to Y. That is, if f is a function with domain X, and codomain Y, one has f ∘ id X = id Y ∘ f = f. {\displaystyle f\circ \operatorname {id} _{X}=\operatorname {id} _{Y}\circ f=f.}