Zagier proved that the traces of singular moduli, i.e., the sums of the values of the classical j-invariant over quadratic irrationalities, are the Fourier coefficients of a modular form of weight 3/2 with poles at the cusps. Using the theta correspondence, we generalize this result to traces of CM values of (weakly holomorphic) modular functions on modular curves of arbitrary genus. We also st...