I'm familiar with the following formula for expected value:
$E(X) = \sum_{x=1}^{\infty} x P(X=x)$
My professor said that for a function $g(X)$ it's true to write:
$E(g(X)) = \sum_{x=1}^{\infty} g(x) P(X=x)$
And he said that we can prove this using the definition but it's not so easy.
But, why this needs to be proved at all? I mean if $X$ is a random variable so is $g(X)$ so we just need to put $g(X)$ instead of $X$ in the definition and that's a clear and direct proof...
What's wrong with my approach and is there any formal proof?
As others have mentioned considering $Y = g(X)$ as a different random variable and looking at $E(Y)$ we get $$E(g(X)) = E(Y) = \sum_{y}yP(Y=y) = \sum_{y}yP(g(X)=y)=\sum_{x}xP(g(X)=x)$$ Obviously that didn't get us anywhere.
Anyways I'll write a proof I find intuitive.
Let $[P]=1$ if $P$ is true, else $[P]=0$. Now notice that $$P(Y=y) = \sum_{x}P(X=x)\cdot [g(x) = y] = \sum_{\{x\ |\ g(x) = y\}}P(X=x)$$ (Basically to count $P(Y=y)$ go through all the values of $x$ and consider the probability contribution of only those for which $g(x)=y$, i.e we count the probability of $Y$ through $X$)
Knowing this we are basically done \begin{align} E(g(X))&=E(Y)\\ &=\sum_{y}yP(Y=y) \\ &=\sum_{y}y\sum_{\{x\ |\ g(x)=y\}}P(X=x) \\ &=\sum_{y}\sum_{\{x\ |\ g(x)=y\}}yP(X=x) \\ &=\sum_{y}\sum_{\{x\ |\ g(x)=y\}}g(x)P(X=x) \\ &=\sum_{x}g(x)P(X=x) \\ \end{align}
Edit: Apparently this does not work in general with discrete random variables, read the comments for further clarification.