MathGroup Archive 2004

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

Search the Archive

Re: Do Modules Produce Side Effects?


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


  • Prev by Date: Re: Do Modules Produce Side Effects?
  • Next by Date: Re: Working with binaries
  • Previous by thread: Re: Do Modules Produce Side Effects?
  • Next by thread: RE: Do Modules Produce Side Effects?