Return
Usage Message: Return[expr] returns the value expr from a function. Return[ ] returns the value Null.
Attributes[Return] = {Protected} Related Symbols: Break
Catch
Continue
Throw
Notes: Return[expr] is used to return expr from a function that you have defined, or to exit from an enclosing
CompoundExpression
, Do
, For
, Scan
, or While
expression. Return[] is equivalent to Return[Null]. When Return[expr] occurs as the result of applying a rule that you have defined, the return value becomes expr.
In[1]:= f[p_] := If[p == 0, Return[False], Return[True]]
In[2]:= f[1]
Out[2]= True
In the example above, the return value is the same even if Return is omitted. The expression If[1==0,Return[False],Return[True]] by
itself, however, evaluates to Return[True]. In many common examples like this, Return is unnecessary. When Return[expr] is
encountered as the value of the body of a Do
loop, or as a result in Scan
, the enclosing Do
or Scan
exits and returns expr. The return value of Do
and Scan
is otherwise normally equal to Null
.
In[3]:= Do[If[k == 3, Return[k], Print[k]], {k, 99}]
1
2
Out[3]= 3
In[4]:= Scan[If[# == 3, Return[#], Print[#]] &, {2, 4, 5, 3, 1, 7}]
2
4
5
Out[4]= 3
When Return[expr] is encountered in the body of a For
or While
loop, or as an element in a CompoundExpression
expression, the enclosing For
, While
, or CompoundExpression
exits and returns Return[expr].
In[5]:= For[k = 1, True, k++, If[k == 3, Return[k], Print[k]]]
1
2
Out[5]= Return[3]
In[6]:= k = 0; While[True, If[++k == 3, Return[k], Print[k]]]
1
2
Out[6]= Return[3]
In[7]:= Print[1]; Print[2]; Return[3]; Print[4]; Print[5]
1
2
Out[7]= Return[3]
In all other situations, Return[expr] is handled just like any other expression. For example, if Return[expr] occurs
during evaluation of Map
or Table
, evaluation continues without change, and the result is given in terms of Return[expr].
In[8]:= Map[If[# > 3, Return[#], #] &, {1, 2, 3, 4, 5}]
Out[8]= {1, 2, 3, Return[4], Return[5]}
In[9]:= Table[If[k > 3, Return[k], k], {k, 5}]
Out[9]= {1, 2, 3, Return[4], Return[5]}
Return[expr] can be thought of as a special version of Throw
and Catch
, where expr is thrown through enclosing CompoundExpression
, For
, or While
expressions, and caught by the nearest enclosing Do
or Scan
, or when it occurs as the result of a rule that you have defined. For example, in the program
In[10]:= Do[
k = 1;
While[k < = n,
Print["k=", k, " n=", n];
If[k == 3, Return[{k, n}]];
k++
],
{n, 1, 99}]
k=1 n=1
k=1 n=2
k=2 n=2
k=1 n=3
k=2 n=3
k=3 n=3
Out[10]= {3, 3}
Return[{k, n}] occurs as the result of evaluating an If
statement, after which it causes an exit from the enclosing CompoundExpression
expression, from the While
loop, from the CompoundExpression
expression which forms the body of the Do
loop, and finally, from the Do
loop. It is useful to keep in mind that Return[expr] does not cause an exit from expressions other than those mentioned above. For example,
if the body of a Do
loop is enclosed in curly braces, so that it becomes a List
expression, the loop does not exit.
In[11]:= Do[{If[k == 3, Return[k], Print[k]]}, {k, 5}]; 0
1
2
4
5
Out[11]= 0
Return can also be used in the form Return[expr, h], where h specifies the head of the expression from
which to exit. Here are examples in which this form is used to exit from a Module
expression, and from Map
.
In[12]:= Module[{k = 5},
Print["start"];
If[k > 0, Return[k, Module]];
Print["end"]
]
start
Out[12]= 5
In[13]:= Map[If[# > 0, Print[#], Return[#, Map]] &, {1,2,3,-1}]
1
2
3
Out[13]= -1
For Return[expr, h] to have the intended effect, it must be evaluated as part of applying a rule for h. In
most cases it is possible to get more flexible control over the flow of a Mathematica program using Throw
and Catch
rather than Return.
Additional Online Documentation:
Mathematica 3.0
http://documents.wolfram.com/v3/RefGuide/Return.html
Mathematica 4.0
http://documents.wolfram.com/v4/RefGuide/Return.html
Questions or comments? Send email to support@wolfram.com.
| |