Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Having found out about the second argument to Return I played a bit with it, and the following tests all resulted in Return::nofunc:





On the other hand, the following works fine:


Does that mean the second argument of Return can only be used with a predefined list of symbols? If so, is there an easy way to find out that list? Otherwise, what am I doing wrong (and how would I do it right)?

share|improve this question
Try this f[x_] := g[Return[x, f]]; f[3] – Dr. belisarius Jun 14 '12 at 15:57
Versus this f[x_] := g[Return[x]]; f[3] of course – Dr. belisarius Jun 14 '12 at 15:59
That (the version of your first comment) also gives Return::nofunc — which is no surprise given that that's equivalent to the case specifically mentioned as error in – celtschk Jun 14 '12 at 16:01
@ celtschk Not in my machine ... it returns 3 – Dr. belisarius Jun 14 '12 at 16:06
Related: (29353) – Mr.Wizard Jul 28 '13 at 9:13
up vote 45 down vote accepted

Ok, my view on this is consistent with all of the cases presented so far...

The symbols allowed in Return are those which are responsible for a transformation in the partial evaluation of the expression so far. Of course, I'm referring only to the evaluation of the branch that includes the Return. There's a subtlety but I'll mention it later

Let's clarify this with your examples:


f has no attributes so, Return is run before any transformation is made. Doesn't work


Again, Return[3, f] is executed first.


Here, i is HoldAll so now we start talking. The i[Return[3,f]]->f[Return[3, f]] is first made. i was the symbol responsible. Now, we run f[Return[3, f]], but f has no attributes, so the Return[3, f] is run, and again, can't find f. Could have found i however.

Last one (actually second on your list, but I thought I'd leave it for last). Here comes the subtlety: Actually, not all symbols responsible for transformations are available. When there's a transformation associated with a symbol, say, s1, and then there's another one AT THE SAME LEVEL, associated with the symbol s2, then s1 is shadowed by s2 and no longer available


g is HoldAll, so that's a good start. First step: g[Return[3, g]]->Return[3, g] and g is responsible for the transformation... So, it would seem that we are succeeding. HOWEVER, the very last transformation from Return[3, g] to whatever, makes the symbol Return shadow g. So, this could be fixed by just making the Return work at a lower level

g[x_]:=# &[x]

It is interesting to note that Return[2, Return] works, and would have worked in the previous example too.

Ok, now let's go to the cases where it actually works


Module is HoldAll. First, there's a transformation associated to Module. If that transformation returned Return[3, Module], then this wouldn't work. But this works. So, I choose to believe that despite what Trace shows, Module evaluated the Return expression internally before returning, just like our g[x_]:=# &[x] example.

@belisarius on the comments:

f[x_] := g[Return[x, f]];

f[3]->g[Return[3, f]]. f was responsible. Now, Return[3, f] is evaluated (at a lower level in the expression tree) and success.

Bonus example:

SetAttributes[f, HoldAll];
g /: f[i_, g] := {i}


f[Return[2, g], g]

This actually works! Why?

f[Return[2,g], g] is turned into {Return[2, g]} BECAUSE OF g. Now, Return is evaluated (at a lower level) and that's the way the cookie crumbles


Imagine the expressions as trees, and the evaluation procedure as a succession of trees, and in each transformation, draw a sign pointing at the new sub-tree, with the symbol responsible written on it. If you ever have to transform that same subtree again, the previous sign is lost. When it's time for Return to evaluate, you go up the tree and see the signs in the path to the top. Those are the ones you can use.

share|improve this answer
Will have to experiment with this more, but a big +1 in any case. – Leonid Shifrin Jun 20 '12 at 4:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.