Return["expr"] in functions and nested loops
- To: mathgroup at smc.vnet.net
- Subject: [mg36929] Return["expr"] in functions and nested loops
- From: Emilio Martin-Serrano <EMartinSerrano at houston.sns.slb.com>
- Date: Wed, 2 Oct 2002 03:32:27 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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