Re: Re: Re: Return in function

*To*: mathgroup at smc.vnet.net*Subject*: [mg106087] Re: [mg106045] Re: [mg106018] Re: Return in function*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Fri, 1 Jan 2010 05:34:17 -0500 (EST)*References*: <hftbe9$cjh$1@smc.vnet.net> <200912300915.EAA17319@smc.vnet.net>*Reply-to*: drmajorbob at yahoo.com

Explanations hidden in technical reports do not count as documentation, and neither do private communications between experts. (For the same reason.) If the behavior of Return is not a bug, it should be documented where users are likely to see it when looking up "Return" in Help. The explanation below seems to be "retrospective cataloging of observed behavior", not "designed behavior". > This seems to explain the behavior of Return in all cases (at least in my > experience). Exactly. Your next experience may be different, and you'd simply add it to the list. Bobby On Thu, 31 Dec 2009 02:15:35 -0600, Leonid Shifrin <lshifr at gmail.com> wrote: > Norbert, > > Had you followed the development of this thread and you would have > noticed > my post where I argued that this is *not* a bug, although indeed a rather > unintuitive feature. The main authoritative source of information on this > matter (apart from the standard documentation) seems to be the technical > report by David Withoff named "Mathematica internals", of 1992. After > making > my post I was additionally informed by Fred Simons that the behavior of > Return has been very nicely summarized by Allan Hayes back in 2002. I > think > this may be of general interest so I repeat it here: > > If Return[x] is generated as a value in Do or Scan then x is immediately > returned; > If Return[x] is generated as an entry in CompoundExpression or as a > value of > the body of a While or For loop then Return[x] (not x) is immediately > returned; > If Return[x] is generated as the value of a user-defined function then > the > function returns x (not Return[x]) > Otherwise Return[x] behaves as a ordinary expression. > > This seems to explain the behavior of Return in all cases (at least in my > experience). > > Regards, > Leonid > > > > On Wed, Dec 30, 2009 at 12:15 PM, Norbert P. <bertapozar at gmail.com> > wrote: > >> On Dec 11, 3:46 am, dh <d... at metrohm.com> wrote: >> > Version 7.0.1 >> > >> > Hello, >> > >> > I am not sure if this is a feature or a bug. If you use Return in an >> > >> > anonymous function, not only the return value is returned, but also >> > >> > "Return" itself. Consider: >> > >> > (If[# == 2, Return[a]; #, #]) & /@ {1, 2, 3} >> > >> > this gives: >> > >> > {1, Return[a], 3} >> > >> > The same thing happens with: >> > >> > Function[x, If[x == 2, Return[a]; x, x]] /@ {1, 2, 3} >> > >> > However, the following works as expected: >> > >> > f[x_] := (If[x == 2, Return[a]; x, x]); >> > >> > f /@ {1, 2, 3} >> > >> > Daniel >> >> Even though I think that it's a bad habit to use Return, it might be >> useful at times. And since it's been in Mathematica since the version >> 1, it should've been fixed a long time ago. >> >> To make the code even simpler, try (version 6.0.2): >> >> In[1]:= f[]:=(Return[a];1); >> >> In[2]:= f[] >> Out[2]= a >> >> It works as expected. Now try >> >> In[3]:= (Return[a];1)&[] >> Out[3]= Return[a] >> >> This is obviously a bug. One would expect to see "a" if Return worked >> properly, or "1" if Return didn't work inside Function, but only in >> definitions such as f[]:=... above. >> >> I get a similar result using RuleDelayed as in >> >> In[4]:= 2/. 2:>(Return[a];1) >> Out[4]= Return[a] >> >> The documentation is also contradictory. In the description of Return, >> there's an example that shows that Return exists only the innermost >> loop (construct) such as Do. Much like Break[], why isn't there Break >> [expr] instead? But in tutorial/LoopsAndControlStructures they say: >> Return[expr] return the value expr, exiting all procedures and loops >> in a function >> >> Best, >> Norbert >> >> > > -- DrMajorBob at yahoo.com

**Re: Re: Weird localization bug!**

**Re: Re: Replace and ReplaceAll -- simple application**

**Re: Re: Weird localization bug!**

**Re: Re: Re: Return in function**