Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Documentation of Return is a little confusing?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113428] Documentation of Return is a little confusing?
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Thu, 28 Oct 2010 04:27:12 -0400 (EDT)
  • Reply-to: nma at 12000.org

?Return says (under More information)

"Return[expr] exits control structures within the definition of a 
function, and gives the value expr for the whole function."

Notice, where it says "gives the value expr for the whole function", 
again, it says "__whole function__" !

Therefore, on writing the following function, and calling it using f[], 
what do you expect the result to be?

----------------
f[]:=Module[{i},

For[i=1,i<= 2,i++,
    {
      If[True,Return[True]];
    }
];

False
]
---------------------

On reading the help, one expects True to be resturned, right? but no:

In[4]:= f[]
Out[4]= False

Not only is the above confusing enough, but removing the {} after For 
changes the behaviour

-----------------
In[6]:= f[]:=Module[{i},

For[i=1,i<= 2,i++,
    If[True,Return[True]];
];

False
]
--------------------

Now,

In[7]:= f[]
Out[7]= True

Ok, now before someone screams at me telling all the above can be easily 
explained by reading what it says under "possible issues":

"Return exits only the innermost construct in which it is invoked"

I do know this. And I do know that the above explains all.

But my point is that the documentation of Return is conflicting with 
itself.

In one place, it says

         "gives the value expr for the whole function"

and in the other place it says

"Return exits only the innermost construct in which it is invoked"

The above 2 statments, at least for the casual user, can not mean the 
same thing? Or Am I not seeing something very deep here?

--Nasser


  • Prev by Date: Re: Researchers break speed barrier in solving important class of
  • Next by Date: Re: Importing data
  • Previous by thread: How to make Mathematica automatically save a notebook to the same
  • Next by thread: Re: Documentation of Return is a little confusing?