posted 22 years ago
When you call a method, program execution leaves the caller, and goes to that method. At some point, the method will finish, and then control "returns" back from where it originally came.
for example, two methods:
public void callingMethod()
{
doMethodCall(); //at this line, program execution "jumps" to the doMethodCall() method
}
public void doMethodCall()
{
//statements....
//when there are no more method statements, the method is done, so it returns. In this case, it returns back to the callingMethod(), since that is the method that called this one.
//You can also return explicitly, with a return statement:
return; //this immediately returns control to the caller.
}
If you have a method that declares that it returns a value, you put the value you are returning to the right of the return statement.
public int howManyFingersOnOneHand()
{
return 5; //control returns , AND the value 5 is returned to the calling method.
}
if I called the above method like so:
int fingerCount = howManyFingersOnOneHand();
the value 5 is returned from the method, and stored in the variable fingerCount.
I hope this helps!