MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Return["expr"] in functions and nested loops


Group,

in the help browser (V4.2)  there is the following about Return[]:

(* Return ["expr"] returns the value "expr", existing all procedures and 
loops in a function *).

Besides, from 2.5.9 Loops and control structures (also in Help browser) we 
can conclude that "While",  "Do",  "Module", "With", etc. must be 
understood as  loops and/or control structures.

But let us have a look at these two function definitions:

yes[a_]:=With[{Ever=True},While[Ever,If[a==7,Return["Terminate"] 
];Print["loop"]]],

In (*yes[]*), Return[] exits the function breaking all loops and control 
structures, and yields "Terminate" as expected.

not[a_]:=With[{Ever=True},While[Ever,While[Ever,If[a==7,Do[Return["Terminate"];Print["foo"] 
]];Print["loop"]]]],

However, in (*not[]*), Return[] only breaks the Do[] construct and keeps 
looping inside While[].

To my understanding, this contradicts that " Return ["expr"] returns the 
value "expr", existing all procedures and loops in a function " as stated 
in the Help Browser. From the Help, any function should be terminated when 
executing Return[] anywhere at any deep of nested looping constructs inside 
the function.

I would appreciate any feedback.

Emilio Martin-Serrano


  • Prev by Date: FindRoot on complex 'interval'
  • Next by Date: Mathematica stole my X so I had to kill it
  • Previous by thread: RE: FindRoot on complex 'interval'
  • Next by thread: Mathematica stole my X so I had to kill it