Re: Null Return for a Module
- To: mathgroup at smc.vnet.net
- Subject: [mg88690] Re: Null Return for a Module
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Mon, 12 May 2008 06:57:00 -0400 (EDT)
- References: <g0903j$kpp$1@smc.vnet.net>
Hi,
try
Test[] := Module[{i, thesum}, thesum = 0;
For[i = 1, i <= 3, i++, thesum = thesum + i]; (* <= Here *)
Return[thesum]]
For[] return Null and since you have forgotten the CompoundExpression[] ";"
the result is interpreted as a product For[__]*Return[_]
and since Return[] does not work in expressions, you
get Null*Return[6]
Regards
Jens
Dave the Funkatron wrote:
> Hi all,
>
> I'm getting strange output and am trying to track down what might be
> causing it. I have the following code in my .nb file:
>
> (* begin code *)
>
> Test[ ] := Module[ {i, thesum},
> thesum = 0;
> For[ i = 1, i <= 3, i++,
> thesum = thesum + i
> ]
> Return[ thesum ]
> ]
>
> Test[ ]
>
> (* end code *)
>
>
> The output I would expect would be simply
>
> 6
>
> But, I get
>
> Null Return[6]
>
> Alternatively, if I change the last line of the Test function to be
>
> thesum
>
> instead of
>
> Return[ thesum ]
>
> I get
>
> 6 Null
>
> as the output. If I remove the for loop, I get expected behavior,
> i.e.,
>
>
> Test[ ] := Module[ {i, thesum},
> thesum = 1 + 2 + 3;
> (*
> For[ i = 1, i <= 3, i++,
> thesum = thesum + i
> ]
> *)
> Return[ thesum ]
> ]
>
> yields the output
>
> 6
>
> as one might expect. I'm not sure what this all means, or what it is
> complaining about. I have other for-loops that are apparently working
> properly. So, can anyone give me an idea what might be going wrong
> here?
>
> Thanks.
>
> Dave
>
>
>