Why can't you take the meaningless loop out of Function 5 and make it inline? There's no point in counting up from 0 until you reach the parameter value. Just rewrite it to look for the @RatingID.
Also, what is the point of creating the table variable in Function 4? Just query against the Rating table joined to Customer, and it can be inline.