If a random variable is a function, then how do we define a function of a random variable I have sometimes seen the formulas such that $\operatorname{Var}(aX+b)=a^2\operatorname{Var} X$ and $EX^2=\sum_{x=0}^nx^2\binom{n}{x}p^x(1-p)^{n-x}$. But how do we define $aX+b$ and $X^2$ if, say $X\sim \operatorname{binomial}(n,p)$, is given? Or in general case.
 A: If $X$ is a random variable, that means that $X$ is a function from a sample space $S$ to the real numbers. If $g:\mathbb{R}\to\mathbb{R}$, we can define a new random variable $Y=g(X)$, so that if $s\in S$ and $X(s)=x$ then $Y(s)=g(x)$.
So as a concrete example, let $X$ be a random variable representing the number of heads that come up in two tosses, and let $Y=X^2$. Then we have
s    | X(s) | Y(s)
TT   |   0  |    0  
TH,HT|   1  |    1  
TT   |   2  |    4

A: As an example, assume you toss a fair coin ten times and that $X$ is the number of heads.  Then sometimes you will have 5 times head, sometimes 6 heads ... out of the 10 coin tosses.   
The number of heads out of 10 coin tosses is thus random: you can not say in advance how many heads you will have, however you know 'something' about it: it must be somewhere between 0 and 10 heads, and for each outcome you can compute the probability that it occurs, nl the probability to observe e.g. 4 heads is $\binom{10}{4}0.5^4(1-0.5)^{10-4}$.    
In this case, if you denote the number of heads that you get in ten coin tosses as $X$, then $X$ is a binomial variable of size 10 and with a success probability of $p=50\%$ so $X \sim Bin(n=10,p=0.5)$.  
Now, let us analyse this further:  if I toss ten coins, then I can have 4 heads in several ways : HHHHTTTTTT (Head=H, T= not head), but also TTTTTTHHHH, ...
So the random variable is a function that 'maps' the combinations of ten H/T onto a number, namely the number of H's among all 10. This is what is meant by $X: S\to \mathbb{R}$, the set $S$ is the set of all possible n-tuples (n=10) of H,T's and one such n-tupple is mapped onto a real number (hence $S \to \mathbb{R}$) by counting the number of H's among the 10. 
So $X$ maps e.g. "HTTTTTTTTT" to $1$ or $X(HTTTTTTTTT)=1$, $X(HHHHHHHHHH)=10$, ...
Note that the outcome can be $1$ in several H,T n-tupples, HTTTTTTTTT, THTTTTTTTT, ... or in terms of functions, the inverse of $1$ under the function $X$ is a set of n-tupples: $X^{-1}(1)=\{HTTTTTTTTT,THTTTTTTTT, \dots TTTTTTTTTH  \}$, and if you now look at the binomial densities, then the fact that $X^{-1}(1)$ is a set is excatly the reason for the factor $\binom{n}{x}$ in your formula.  
So a random variable is a map that maps the outcome of a random experiment onto a real number. 
What is now the random variable $5X$ ? 
Well it is a random variable that is the number of heads (in 10 coins tosses) multiplied by 5.  It is a random variable because you can not tell in advance what the outcome will be, you know the possible outcomes (0, 5, 10, ... 50) and for each outcome you can compute the probability. 
The random variable $5X$ is a map (a function) from the set of the possible combinations of ten H/T onto a number, namely five times  the number of H's among all 10. 
