MathGroup Archive 2004

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

Search the Archive

RE: Do Modules Produce Side Effects?

  • To: mathgroup at
  • Subject: [mg47992] RE: [mg47964] Do Modules Produce Side Effects?
  • From: "David Park" <djmp at>
  • Date: Wed, 5 May 2004 08:10:59 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


But you didn't include m in the list of local Module variables. Therefore it
treated it as the already defined Global variable.

m = i^2;

Module[{m = 4}, 2m]

For a case like this, the more efficient statement is probably

With[{m = 4}, 2m]

With, as the first step, replaces every occurrence of m in the body of the
expression by 4. Module creates a new internal variable and initializes it
to the value 4. It replaces every occurrence of m in the body by the new
internal variable. If we don't give the new internal variable a value, then
it will return the new internal variable.

Module[{m}, 2m]
2 m$29

Block creates a new value (but not variable) for m, which is only used

Block[{m = 4}, 2m]

If we don't give m a value, which is really a misuse of Block, then we see
that the original value of the original variable is used.

Block[{m}, 2m]
2 i^2

David Park
djmp at

From: Harold Noffke [mailto:Harold.Noffke at]
To: mathgroup at

$Version "5.0 for Microsoft Windows [2000] (November 18, 2003)"


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


  • Prev by Date: RE: Re: Re: bug in IntegerPart ?
  • Next by Date: RE: Re: bug in IntegerPart ?
  • Previous by thread: RE: Do Modules Produce Side Effects?
  • Next by thread: Re: Three graph into one