MathGroup Archive 2010

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

Search the Archive

Re: local variables - Module, For loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113037] Re: local variables - Module, For loop
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Tue, 12 Oct 2010 04:23:28 -0400 (EDT)

What do you think the purpose of that first (list) argument to Module is 
for -- just to annoy you?  Why even bother to use Module if you don't 
intend to use it to isolate some local variables?

In short, the answer is, "yes, you do need to put the variables intended 
to be local into that first-argument list."

On 10/11/2010 5:15 AM, Sebastian Schmitt wrote:
> Dear all!
>
> (I recycle my disclaimer.)
>
> I'm new to Mathematica with a background mostly in C++. Many times I
> have the impression that my style is not natural-Mathematica
> (Mathematicaesque so to say).
>
> If I have a For loop in a function like this:
>
> In[39]:= f[x_] := Module[{},
>    For[i = 0, i != x, i++,
>     Print[i]
>     ]
>    ]
>
> In[41]:= f[2]
>
> During evaluation of In[41]:= 0
>
> During evaluation of In[41]:= 1
>
> In[42]:= i
>
> Out[42]= 2
>
> I was surprised to find "i" being not local to the For loop. Do I have
> to keep track of all my throw-away-variables and put them in the list of
> local variables of the Module? I find it pretty tedious. Is there a
> better way?
>
> Thanks in advance,
>
> Sebastian
>

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: local variables - Module, For loop
  • Next by Date: Re: local variables - Module, For loop
  • Previous by thread: Re: local variables - Module, For loop
  • Next by thread: Re: local variables - Module, For loop