Re: Do Modules Produce Side Effects?
- To: mathgroup at smc.vnet.net
- Subject: [mg48002] Re: Do Modules Produce Side Effects?
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Wed, 5 May 2004 08:11:07 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 5/4/04 at 1:08 AM, Harold.Noffke at wpafb.af.mil (Harold Noffke)
wrote:
>The MathBook definition of Module tells me, "Module creates new
>symbols to represent each of its local variables every time it is
>called." I am led by this, and other Module descriptions, to
>conclude Modules do not produce side effects, like Blocks do.
>However, we have ....
>In[1]:= m=i^2
>Out[1]= i^2
>In[2]:= Module[ {}, m=4; 2*m ]
>Out[2]= 8
>In[3]:= m Out[3]= 4
>I expected m to remain unchanged from its original i^2. But Module
>changed m to 4, just as I would expect a Block to do.
>Am I misunderstanding something about the "side effect safety" of
>Modules?
I think you are misunderstanding the syntax of Module. To get be able to use m as a local variable and preserve global values of m, you need to specify m to be a local variable. That is you need to use the following to get the behavior you want
Module[{m}, m=4; 2 m]
--
To reply via email subtract one hundred and four