2
Answers

the role of void and return keyword

Ask a question
Hi Guys!
When  try to answer the questions to prepare for text, I got the question like this: Consider the C# method below. What happens if the keyword return is removed?


public void FireCannonBall()
{
        foreach ( GameObject ball on cannonBalls)
        {
                if (!ball.alive)
                {
                        ball.alive= true;
                        ball.position= cannon.position-ball.center;
                        ball.velocity= new Vector2((float)Math.Cos(cannon.rotation),(float)Math.sin(cannon.rotation))* 5.0f;
                return;
                }
        }
}


I my opinion,if the keyword return is removed, we will meet the error.This class is void class, so it is cannot return. we remove the keyword return,the compiler will not understand and do not return ball.velocity to float type.
Is that the right answer????

Could you please help me to find the correct answer for the question? 

Regards
Jonny



Answers (2)