MathGroup Archive 1998

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

Search the Archive

Return inside Do, For and While



I am going through the Power Programming book by Wagner and have a
question about Return[value].

According to the documentation, it is supposed to cause a "function" to
return a value.  I have always used it to exit and return a value from
a Module or With.  Because I almost never use Do, For or While except
when following a textbook, I had never tried to use Return from inside
a loop.

For example, consider the following code that tests a list of integers
to see if they are all odd:

allOdd[x_List]:=Module[{i=1},

	Do[If[!OddQ[x[[i]]],
	      Return[False]];
	    i++,
	{Length[x]}];

		Return[True]]

This function returns True no matter what the list is.  Consider the
following modification:

allOdd[x_List]:=Module[{i=1},
    	If[!Do[If[!OddQ[x[[i]]],
	Return[False]];
	i++,
    {Length[x]}],
Return[False]];

		Return[True]]

This makes it clear that Wolframs definition of a "function" includes
loops (Do, While, For).

Does anyone know what other built in objects qualify as "functions" for
purposes of using Return?



  • Prev by Date: expressions as arguments to functions.
  • Next by Date: Re: Problem with Limit
  • Prev by thread: Re: expressions as arguments to functions.
  • Next by thread: Re: Return inside Do, For and While