MathGroup Archive 2006

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

Search the Archive

Re: Variables Within Homemade Functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69593] Re: Variables Within Homemade Functions
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 17 Sep 2006 06:57:02 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eegb90$lm$1@smc.vnet.net>

Gregory Lypny wrote:
> Hello everyone,
> 
> How can I ensure that variables in my home made function do not  
> conflict with variables with the same name outside the function?  Say  
> I have a variable, t, whose value in my notebook is 17.  I've brought  
> in a function from another notebook, myFunction, that contains a  
> variable named t, and in running the function, t will take on a  
> value.  Unfortunately, both t's are the same.  Is there any way to  
> make the t within the my function a local variable just like index  
> counters in the Table and Do functions?
> 
> Regards,
> 
> 	Greg
> 
> t=17
> 
> myFunction[x_]:=(t=someOtherValue; rest of function here)
> 

Gregory,

Module [1] is your friend here:

In[1]:=
t = 17;

myFunction[x_] :=
   Module[
     {t = someOtherValue},
     Print["my local t is equal to ", t]
   ];

Print["t is equal to ", t];
myFunction[2];
Print["t is equal to ", t];

 From In[1]:=
t is equal to \[InvisibleSpace]17

 From In[1]:=
my local t is equal to \[InvisibleSpace]someOtherValue

 From In[1]:=
t is equal to \[InvisibleSpace]17

Also, you should have a look at With and Block.

Regards,
Jean-Marc

[1] http://documents.wolfram.com/mathematica/functions/Module


  • Prev by Date: Re: A new application for Mathematica
  • Next by Date: Re: How to combine 3 grayscale images into one RGB color image ?
  • Previous by thread: RE: Variables Within Homemade Functions
  • Next by thread: Re: Variables Within Homemade Functions