MathGroup Archive 2010

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

Search the Archive

Re: Why Return[] does not work?

  • To: mathgroup at
  • Subject: [mg109495] Re: Why Return[] does not work?
  • From: Fred Simons <f.h.simons at>
  • Date: Fri, 30 Apr 2010 05:49:43 -0400 (EDT)
  • References: <hr6ihp$2f8$> <>

The function Return turns up in this discussion group very regularly. 
Already in January, 2002 Allan Hayes posted in this group the following 
summary of the behaviour of Return, that has not lost anything of its 
importance since then and that answers all questions raised so far:

If Return[x] is generated as a value in Do or Scan then x is immediately 
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.

Allan did not include the use of Return to end a Dialog session. In that 
case, Return should be used as a stand alone command, not e.g. in a 
compound expression or in a button.

My feeling is that the intended purpose of the function Return is to 
exit from a user-defined function with the argument of Return as the 
value of the function call. That is a relict from programming languages 
a long time ago, when indeed it was necessary to use this command to get 
a value out. There is no reason to use this command in this way 
nowadays. But it explains e.g. why the result of a compound expression 
with a Return argument has to be a Return expression. In the following 
artificial example we have two nested compound expressions:

In[63]:= f[___] := Module[{y=0}, y=y+1; If[y>0, 

Out[64]= 2

During the evaluation of the inner compound expression we arrive at 
Return[y], so we want to have that value of y (that is 2) as the result 
of the function call. If y was not wrapped in Return, the outer compound 
expression would be completely evaluated, resulting in the value 102 
instead of 2.

For the same reason the result of While and For with a Return is wrapped 
in Return. The fact that Do and Scan behave differently does not seem 
very very consequent. Further, Return has no effect at all when it turns 
up in the result of a Table or a Map command, two other loop constructs.

I think that Return is not intended for interrupting loops. The 
documentation states that Return[expr] returns the value expr from a 
function. Not a very clear description, but in line with the above 
remarks. As many have already said, for interrupting loops the 
Catch-Throw construction is much more appropriate.

Finally two more examples of 'strange' behaviour:

By defining a function with Blank's, we create a user defined rule, and 
Allan's survey also holds for user defined rules. Consider the immediate 
assignment a=Return[3]. Evaluation of the right hand side of course 
returns Return[3]. But since we have created a rule for the symbol a 
(see OwnValues[a]), calling a gives 3 instead of Return[3].

A pure function does not create a user defined rule, so in pure 
functions Return will be persistent:

In[26]:= f[x_] := Return[x];
{f[a], g[a]}

Out[28]= {a,Return[a]}

Fred Simons
Eindhoven University of Technology

  • Prev by Date: Re: Odd phenomenon
  • Previous by thread: Re: Why Return[] does not work?
  • Next by thread: Re: Why Return[] does not work?