When I teach beginners about YTM I always make a point to describe it as the Promised Yield to Maturity, which I abbreviate as PYTM.
Mathematically the PYTM is computed in the usual way, as Will Gu said. But for a risky bond the PYTM is not a measure of "how much money I am going to earn on this bond", but rather the best that you can do if the company is able to come through and make the payments it has promised. Obviously for a risky company the probability of this happening is less that 1, perhaps much less than 1 for a seriously troubled company, so you expect to make less than this amount on average.
If you are interested in the expected return on the bond the PYTM is not useful and you have to pursue an approach like the one shown by phdstudent above where you take expected values of the cash flows. You then have a different concept, which you can call Effective YTM or something like this.
Be very careful not to misinterpret the [P]YTM for a risky bond when you see it quoted (and they are quoted all the time). The interpretation is different than for a risk free bond.