MathGroup Archive 2009

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

Search the Archive

Re: Re: Return in function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106045] Re: [mg106018] Re: Return in function
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Thu, 31 Dec 2009 03:15:35 -0500 (EST)
  • References: <hftbe9$cjh$1@smc.vnet.net> <200912300915.EAA17319@smc.vnet.net>

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
>
>



  • Prev by Date: Re: Replace and ReplaceAll -- simple application
  • Next by Date: Re: Weird localization bug!
  • Previous by thread: Re: Return in function
  • Next by thread: Animation with Sound in Mathematica?