Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Where does Return return to?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48631] Re: Where does Return return to?
  • From: ab_def at prontomail.com (Maxim)
  • Date: Tue, 8 Jun 2004 00:48:34 -0400 (EDT)
  • References: <ca1d48$d0r$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Generally speaking, Return is supposed to return a value from a
function, but any Mathematica expression, except atoms, is a function.
If the expression has the form While[True,List[Return[1]]], then it is
not clear which function we should exit from. This naturally leads to
inconsistencies:

In[1]:=
Module[{}, Do[Return[1], {1}]; 0]
Module[{}, While[True, Return[1]]; 0]

Out[1]=
0

Out[2]=
Return[1]

There is an undocumented form of Return using the second argument; it
tells Return which function on the evaluation stack to look for:

In[3]:=
Module[{}, Do[Return[1, Module], {1}]; 0]

Out[3]=
1

Since the head of the returned expression can happen to be Return, you
can nest Return's:

In[4]:=
Module[{}, Do[Return[Return[1]], {1}]; 0]

Out[4]=
Return[1]

But compare:

In[5]:=
Module[{}, Do[Return[Return[1, Do]], {1}]; 0]

Out[5]=
0

Perhaps a more annoying inconsistency (and the explanation of why the
last two examples work the way they do) is that sometimes the
resulting expression is Return[1] and sometimes just 1:

In[6]:=
f[x_] := (If[x == 0, Return[1]]; x)
g = Function[x, If[x == 0, Return[1]]; x];
f[0] == g[0]

Out[8]=
1 == Return[1]

Even if you're lucky enough to guess where Return will land, you still
don't know what format the result will have. Much of the above applies
to Break and Continue as well.

Maxim Rytin
m.r at inbox.ru


  • Prev by Date: LogIntegral^(-1)
  • Next by Date: Re: Problem with the Derivative of a Arg-function
  • Previous by thread: Re: Re: Where does Return return to?
  • Next by thread: Hurst exponent (R/S)