Law 2 in the ICC Law Book states that,
A batsman may retire at any time during his innings when the ball is dead. The umpires, before allowing play to proceed shall be informed of the reason for a batsman retiring.
(a) If a batsman retires because of illness, injury or any other unavoidable cause, he is entitled to resume his innings subject to (c) below. If for any reason he does not do so, his innings is to be recorded as ‘Retired - not out’.
(b) If a batsman retires for any reason other than as in (a) above, he may resume his innings only with the consent of the opposing captain. If for any reason he does not resume his innings it is to be recorded as ‘Retired - out’.
(c) If after retiring a batsman resumes his innings, it shall be only at the fall of a wicket or the retirement of another batsman.
If a batsman falls ill or gets injured while batting, he may retire with the umpires permission. At the end of the teams innings, if this batsman does not return to bat, he is considered 'Retired Hurt' and is considered not-out. Statistics-wise, the runs scored by him will be added to his career total as well as the innings. However, as average is calculated by "Runs scored / ( Innings - Not outs )", the denominator would not be affected by this game, as he is still not out. As long as he has scored a run or more before retiring hurt, it effectively means his career average will be better at the end of the game than it did at the start of it.
This is a scenario where a batsman is not able to come to the pitch to bat even without facing a ball, due to prior illness or injury. In this case, no runs will be added to his career stats and therefore, will not affect this average as neither will the (Innings - not outs) change. So, his batting average prior to the game is the same as that at the end of it.
If a batsman retired without the umpire's consent and if he does not have permission from the opposing captain, then he is considered 'Retired-out'. This is considered as a dismissal. Only two batsmen have retired out in an international cricket Test match. Both instances occurred in the same match, with the Sri Lankan batsmen Marvan Atapattu and Mahela Jayawardene retiring out.