The moment generating function (MGF) of a random variable X is
MX(t)=E[etX]defined for the values of t where the expectation is finite. Expand the exponential inside the expectation and take expectations term by term:
MX(t)=1+tE[X]+2!t2E[X2]+3!t3E[X3]+⋯So the nth moment is the nth derivative at zero, E[Xn]=MX(n)(0). Equivalently, it is n! times the coefficient of tn.
Take X∼Exponential(λ). Then
MX(t)=∫0∞etxλe−λxdx=λ−tλ,t<λThe integral blows up for t≥λ. That is allowed: an MGF only needs to be finite on some interval around 0. Writing λ−tλ=∑n≥0(t/λ)n and matching coefficients gives E[Xn]=n!/λn in one line. With λ=2: E[X]=0.5 and E[X2]=0.5, so Var(X)=0.25, and E[X3]=6/8=0.75. By integration by parts that would be three separate calculations.
For a discrete X the integral becomes a sum. A Poisson(λ) variable gives
MX(t)=k≥0∑etkk!e−λλk=eλ(et−1)and differentiating once at t=0 returns λ, as it should.