Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling 'return' doesn't seem to work immediately?

 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method and inside of an if/then, I have it print the word "done" and then I call return. Problem is that is prints "done" about 100 times instead of exiting the method right away. It does eventually work, but why does it print "done" so many times first? It almost looks like a loop...
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does sound like a loop, yes. could you post the code?
 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too, would like to see the code for this.

But to answer your question... there is a case, where it doesn't return immediately. If there is a finally clause, that code needs to be executed.

Henry
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AS Henry says, there is a case where calling return does not exit the function right away. However, in all other cases, return returns, terminating the flow of the method -- even inside a loop.

In order to accurately explain what is going on, we will need to see your code, butI think that you have a loop somewhere that you are not accounting for.
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Max Simpson:
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?


As others have said, there are cases where "return" doesn't return right away. But I doubt that's the problem here.

It sounds like you might be confused on what "return" returns from. It will cause the method to exit, but it's the method that the return is in. In other words, your function that prints "Done" is what the return will exit from. And when it exits, it winds up right back at the loop your function was called from. That method (the one with the loop) is not at all affected by the return that's in your "Done" method.

- Jeff
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the others have said, posting code snippets of the loop and of the method that has the return statement would help us to help you.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the exact code that is causing this behavior. Without the exact code, we can only guess at what is happening here.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic