[Mathematica] Confusion with Return[]

  • Context: Mathematica 
  • Thread starter Thread starter guerom00
  • Start date Start date
  • Tags Tags
    Confusion Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the behavior of the Return[] function in Mathematica, particularly in the context of defining functions using different notations. Participants explore the differences in output when using Function versus pattern-based definitions, and the implications of these differences on function evaluation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes a discrepancy in the output of two function definitions, where one returns a value and the other displays Return[a] explicitly.
  • Another participant suggests that there is a fundamental difference between defining a function with Function and using the pattern-based definition, although they are unsure if these notations are truly equivalent.
  • A different participant interprets the behavior as a potential bug, providing traces of the function evaluations to illustrate the issue with Return[].
  • Another participant mentions that Return[] has been problematic in various situations, referencing an external example to support this claim.
  • One participant indicates that switching to the pattern-based definition resolves their issue, and they confirm the behavior across multiple versions of Mathematica.

Areas of Agreement / Disagreement

Participants express differing views on whether the two function definitions are equivalent and whether the observed behavior constitutes a bug. The discussion remains unresolved regarding the underlying reasons for the discrepancies in output.

Contextual Notes

Participants reference specific versions of Mathematica (7.0.1 and 8) and provide traces of function evaluations, indicating that the behavior may depend on the version or specific context of use.

guerom00
Messages
90
Reaction score
0
Hello all,

There's something I don't understand when using Return[]…

Take this input :

f := Function[{x}, (If[x > 5, Return[a]]; x + 3)];
g[x_] := (If[x > 5, Return[a]]; x + 3);

and this output :

In[25]:= f[6]
g[6]

Out[25]= Return[a]

Out[26]= a

In one case, it returns the value "a", as expected. But in the other case, it writes explicitly "Return[a]". Why ? I don't understand...

Thanks in advance.
 
Physics news on Phys.org
I think the bottom line is that there must be a difference between defining a function using the notation f=Function[x,body] and the notation f[x_]=body. But I don't know, I thought those two notations were equivalent...
 
It looks like a bug to me...

Code:
In[1]:= f:=Function[{x},(If[x>5,Return[a]];x+3)];
       g[x_]:=(If[x>5,Return[a]];x+3);

In[3]:= f[6]//Trace
Out[3]= {{f,Function[{x},If[x>5,Return[a]];x+3]},Function[{x},If[x>5,Return[a]];x+3][6],If[6>5,Return[a]];6+3,{{6>5,True},If[True,Return[a]],Return[a]},Return[a]}

In[4]:= g[6]//Trace
Out[4]= {g[6],If[6>5,Return[a]];6+3,{{6>5,True},If[True,Return[a]],Return[a]},Return[a],a}

The point of interest is at the very end of the trace:
Code:
f:...  If[True,Return[a]],Return[a]},Return[a]}
g:...  If[True,Return[a]],Return[a]},Return[a],a}
Which is strange. Obviously it is recognizing the Return[] call, because otherwise it would move on to the next command, x+3... as happens when x<=5. It's just not evaluating Return[a] properly.

What version are you running?
 
OK, thanks :)
Well, at least for my purpose, using the f[x_]:=body notation solves the issue…
@Simon_Tyler: I can reproduce this issue both in 7.0.1 and 8 on a Mac.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K