Re: Do Modules Produce Side Effects?
- To: mathgroup at smc.vnet.net
- Subject: [mg47985] Re: Do Modules Produce Side Effects?
- From: Peter Hahn <pg.hahn at arcor.de>
- Date: Tue, 4 May 2004 07:03:17 -0400 (EDT)
- References: <c778ms$hf7$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Harold,
you just have forgotten to declare m in the module as a local variable,
using
Module[ {m}, m=4; 2*m ]
leaves the global m unchanged, as expected.
Peter
Harold Noffke wrote:
> $Version "5.0 for Microsoft Windows [2000] (November 18, 2003)"
>
> MathGroup:
>
> 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?
>
> Regards,
> Harold
>
- Follow-Ups:
- color in SphericalPlot3D
- From: "Maurits Haverkort" <Haverkort@ph2.uni-koeln.de>
- color in SphericalPlot3D