Howdy!
Good question. The 'return' keyword means 'return from this method NOW (popping the method off the stack and returning to the calling method)'. If there *is* a non-void return type, the return MUST be followed by a value or variable that can be legally assigned to the return type. (return 2; return new Dog(); etc.)
So in the code that you have, you're right -- the return statement has no purpose since that's the end of the method anyway. But... you *could* do something like:
void myMethod() {
if (x > y) {
doStuff();
return; // says get out NOW!
}
// continue on
// more code
}
In this context (with a void return type), the 'return' is kind of like a 'break from the whole method' statement.
Of course, this isn't necessarily a good coding practice, but that's a different story...
Bottom line: you can always have a return statement in a method. In a method with a non-void return type, the return MUST be followed with a value/variable that is compatible with the return type of the method.
In either case, return ALWAYS means "get out of the method now and return control to the calling method'".
Except... could there be a case where a return will NOT immediately take you out of the method�where the current method stays on the stack for just a little while longer before the actual 'return' happens?
I'll leave that as an exercise for those of you just starting your studies
So that's the question:
Under what circumstances, if any, does a 'return' statement not cause the current method to pop off the stack immediately?
cheers,
Kathy