How do I find the composition of two functions? In a question sheet for my university course I am given the sets:

$A = \{1, 2, 3, 4\}, B = \{x, y, z, w\}$ and $C = \{a, b, c\}$.

I am then told to work out the functions: 

  
*
  
*$f: A → B$ which is defined by $f(1) = y, f(2) = w, f(3) = z, f(4) = x$.
  
*$g: C → A$ which is defined by $g(a) = 2, g(b) = 3, g(c) = 4$.
  
*$h: A → C$ which is defined by $h(1) = a, h(2) = c, h(3) = a, h(4) = b$.
  

The ordered pairs of these functions is:

  
*
  
*$f: A → B = \{(1, y), (2, w), (3, z), (4, x)\}$
  
*$g: C → A =  \{(a, 2), (b, 3), (c, 4)\}$
  
*$h: A → C = \{(1, a), (2, c), (3, a), (4, b)\}$
  

The above is easy enough but I am then asked to find the composition of these functions. The actual process of doing the composition seems easy enough but deciding which domains/codomains are mapped to each other confuses me. The answers are provided by the sheet I'm working from as:

  
*
  
*$f ∘ g: C → B$
  
*$g ∘ h: A → A$
  
*$h ∘ g: C → C$
  

What is considered when choosing what the domain/codomain maps to? Also, take $f ∘ g$ for example. Why is g's domain used first when it makes sense that $f$ is first so shouldn't $f$'s domain be used? 
 A: By definition, the composite function $(f \circ g)(x) = f(g(x))$. This means that you apply $g$ first, and $f$ second, as in the following diagram:
$$C \xrightarrow{g} A \xrightarrow{f} B $$
So if $g:C \to A$ and $f:A \to B$, the composite $f \circ g:C \to B$.
A: The definition of (say) $f \circ g$ is to "do $g$ first, then $f$" even though we're used to reading left to right.
This is to agree with our notation that $f(x)$ applies the function $f$ to input $x$.
So $(f \circ g)(x)$ means that $g$ gets to "go first" since it's closer; we have $(f \circ g)(x) = f(g(x))$; "apply $f$ to what we get applying $g$ to $x$."
Since $g$ is going first, the domain of $f \circ g$ will be the domain of $g$. Likewise, since $f$ is the final function applied (to the outputs of $g$), our overall codomain will be wherever $g$ sends things.
A: Throughout my studies there sometimes arose the confusion about the symbol $\circ$. In my country (and western countries in general) we read from left to right. So normally the left most thing comes as the first. So, if in words we want to compose the functions $f$ first then $g$ it would be intuitive to write $f\circ g$. But the actual point-wise definition of composition of functions is $g(f(x))$ ($f$ first then $g$). So we get
$$
(f\circ g)(x) :=g(f(x)).
$$
Which might cause more confusion. At our university we are used to define
$$
(f\circ g)(x) := f(g(x)),\tag{1}
$$
so the order of $f,g$ does not change throughout the expression (1). And the second definition is the one I come across most.
So, for $f\colon A \to B, g\colon C \to A$ it makes senses to consider $f\circ g\colon C \to A \to B$ and $g\circ f\colon A \to B \to C$ does not make any sense.
