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
Jean PaulPosted Apr 19, 2011, 4:25 AM
I would like to denote that removing return does not throw error.
The void methods shall also have return statement - this can be used to exit the method while middle of execution.
return is mandatory for non-void methods.
In the above case if return is removed, the loop should continue till all GameObject are processed and then the method will exit.
Regards,
Jean Paul
VulpesPosted Apr 19, 2011, 4:21 AM