Message Boards Message Boards

0
|
9479 Views
|
8 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Return[] in functions

Posted 10 years ago

Hello,

in this example of code:

f1[x_] := Return[x + 1];

f2 := Function[{x}, Return[x + 1]];

f1[1] returns "2", but f2[2] returns "Return[2]". Why doesn't the Return[] operator work correctly in the second function?

POSTED BY: Pavel AZ
8 Replies

Ah yes, code that takes code in its argument. One might like to think that things like that are exceptional cases, but that isn't necessarily so for many of the things one might want to do with Mathematica. So this sort of thing is very worth remembering when one finds behavior that seems "buggy" in code that one has written that processes code--and such cases will invariably have Hold* attributes or make use of Unevaluated and things like that....

POSTED BY: David Reiss

Well, that has its dangers too.

Consider this toy example:

In[5]:= SetAttributes[f, HoldAll]
f[x_] := Catch@Module[{}, If[x < 0, Throw[$Failed]]; Sqrt[x]]

In[7]:= f[-1]    
Out[7]= $Failed

In[8]:= f[2]    
Out[8]= Sqrt[2]

In[9]:= f[Throw[boo]; 2]    
Out[9]= boo

The same caveat applies to Sow/Reap.

This can be fixed by using a (private) tag with Throw and Catch (or Sow and Reap).

POSTED BY: Szabolcs Horvát

Actually I always use

f[...] :=
    Catch@Module[{...},
        ...
        If[error, Message[f::err]; Throw[$Failed]];
        ...
        If[otherError, Message[f::err]; Throw[$Failed]];
        ...
        result
     ]

in those cases ;-) . Part of the reason is just habit, but also it allows me to control exactly where the Throw gets caught without any ambiguity. And one can use tags in appropriately palced Throws and Catches if a more complex logic is needed.

POSTED BY: David Reiss
POSTED BY: Szabolcs Horvát
POSTED BY: David Reiss
Posted 10 years ago

OK, so these two ways of functions declaration aren't equivalent.

POSTED BY: Pavel AZ
Posted 10 years ago
POSTED BY: Alexey Popkov
Posted 10 years ago
POSTED BY: Douglas Kubler
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract