expr = Return[1, 2]

Return::nofunc: Function 2 not found enclosing Return[1,2]. >>

(*Hold[Return[1, 2]]*)

However, Hold[Return[1, 2]] is not assigned to expr.Why does this happen? I want to use some code to store the output into that variable, is it possible?

PS: Please don't manually copy/paste or use methods like read the output cells/notebook contents and its variation. I prefer dealing with the problem directly.

  • $\begingroup$ Have you seen this? $\endgroup$ – J. M.'s discontentment Jul 7 '16 at 4:16
  • $\begingroup$ @J.M. Thanks, that does captures the Expression in error message. But I still wonder why the output is not assigned to the variable. $\endgroup$ – vapor Jul 7 '16 at 4:22

Expressions that simply evaluate with messages are not returned wrapped in Hold, and there is no problem assigning these to a Symbol:

expr = Map[1, 2, 3, 4];


Map::nonopt: Options expected (instead of 4) beyond position 3 in Map[1,2,3,4]. An option must be a rule or a list of rules. >>


expr = Map[1, 2, 3, 4]

Your use of Return however breaks the normal evaluation order much as Throw or Break would, so Set is never completed. This is really no different from wondering why Print does not print "foo" or "bar" in:

Print["foo", "bar", Return[1, 2]]

Return::nofunc: Function 2 not found enclosing Return[1,2]. >>

Hold[Return[1, 2]]

If you explain how this output is originating perhaps I can recommend a work-around.

| improve this answer | |
  • $\begingroup$ Thanks! My original case was that, I was swapping a head to Return in an expression like head@@{1,2} and I wanted to extract information in list {1,2}, but since the assignment failed, I have to change to other methods. $\endgroup$ – vapor Jul 7 '16 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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