Return inside Do, For and While
- To: mathgroup@smc.vnet.net
- Subject: [mg12568] Return inside Do, For and While
- From: Sean Ross <seanross@worldnet.att.net>
- Date: Sat, 23 May 1998 18:11:28 -0400
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?