Skip to main content
Tweeted twitter.com/#!/StackMma/status/590849748665503744
edited tags
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Source Link
Jerry Guern
  • 4.7k
  • 20
  • 53

How to force Return[] to return from an arbitrary point (like in C++)?

I need to test some complicated conditions to calculate a Return value. But MMa doesn't let you Return[] from anywhere the way C++ does. The structure of my Module is similar to this:

foo[x_] := Module[{}, Do[ If[x == (n^2), Return[0]]; If[x == (n^4), Return[1]], {n, 1, 5}]; Return[2]]; 

If Return[] actually Return'd, foo[4] would return 0, but instead it returns 2.

Is there a way to do a return from an arbitrary place in the code?

I know that effete programmers will dislike my program structure, but I'm asking how to force the Return[], not how to restructure the program.