Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Variables Within Homemade Functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69598] Re: Variables Within Homemade Functions
  • From: dimmechan at yahoo.com
  • Date: Sun, 17 Sep 2006 06:57:12 -0400 (EDT)
  • References: <eegb90$lm$1@smc.vnet.net>

***You can use Module or Block.
**Some simple examples will demonstrate their usage.
***I have converted the Outs in InputForm

Quit

Map[Information[#]&,{Module,Block}];
Module[{x, y, ... }, expr] specifies that occurrences of the symbols x,
y, ...  in expr should be treated as local. Module[{x =
   x0, ... }, expr] defines initial values for x, ... .
InputForm[Attributes[Module] = {HoldAll, Protected}]
Block[{x, y, ... }, expr] specifies that expr is to be evaluated with
local values for the symbols x, y, ... . Block[{x = x0,
   ... }, expr] defines initial local values for x, ... .
InputForm[Attributes[Block] = {HoldAll, Protected}]


***First notice that

t=17;

myfunction[x_]:=(t=a;x+c+t)

myfunction[b]
a + b + c

FullDefinition[myfunction]
myfunction[x_] := (t = a; x + c + t)
t = a

Information[t]
Global`t
InputForm[t = a]

***So the variable t take the new value a inside the definition of
myfunction[].
***Here is how you solve the problem.

Clear["Global`*"]

t=17;

myfunction[x_]:=Module[{t=a},x+c+t]

myfunction[b]
a + b + c

FullDefinition[myfunction]
myfunction[x_] := Module[{t = a}, x + c + t]
t = 17

Information[t]
Global`t
InputForm[t = 17]

***Hence using Module the global variable t keeps being equal to 17,
how you wish.
***Another alternative is to use Block.

Clear["Global`*"]

t=17;

myfunction[x_]:=Block[{t=d},x+c+t]

myfunction[b]
b + c + d

FullDefinition[myfunction]
myfunction[x_] := Block[{t = d}, x + c + t]
t = 17

Information[t]
Global`t
InputForm[t = 17]

***However there is a big difference between Block and Module.

Clear["Global`*"]

t=17;

myfunction[x_]:=Module[{t},x+c+t]

myfunction[b]
b + c + t$17

Information[t]
Global`t
InputForm[t = 17]

***The variable t$17 is the local variable used inside the Module.

***On the contrary

Clear["Global`*"]

t=17;

myfunction[x_]:=Block[{t},x+c+t]

myfunction[b]
17 + b + c

FullDefinition[myfunction]
myfunction[x_] := Block[{t}, x + c + t]
t = 17

Information[t]
Global`t
InputForm[t = 17]

***I.e. if you do not assign a value for t inside the Block, Block will
make usage of the global value of t.

***BTW, Block can have amazing results. For example


Integrate[1/x, {x, -1, 2}]
                             1
Integrate::idiv: Integral of - does not converge on {-1, 2}. More...
                             x

Integrate[x^(-1), {x, -1, 2}]

***But

Block[{Message},Integrate[1/x,{x,-1,2}]]
Infinity

***Also

Block[{$DisplayFunction = Identity}, g1 = Plot[UnitStep[x - 1], {x, 0,
    1}]; g2 = Plot[UnitStep[x - 1], {x, 1, 2}]];
Show[g1, g2, Axes -> False, Frame -> True];

***Otherwise you get a buggy vertical line connecting the points a x=1.

***Finally

{x/x,x+x}
{1, 2*x}

***but

Block[{Plus},Apply[HoldForm,Apply[List,HoldForm[{x/x,x+x}]]]]//StandardForm
{1,x+x}


***You can see more applications of Block as well a lot of information
for other Built-In functions of Mathematica in the amazing link
http://www.verbeia.com/mathematica/tips/Tricks.html (owned by Ted
Ersek).
***Of course you should consult first the Mathematica Book!

Regards 
Dimitris Anagnostou


  • Prev by Date: RE: question about plot
  • Next by Date: Re: Plotting with arbitary precision????
  • Previous by thread: Re: Variables Within Homemade Functions
  • Next by thread: Re: Re: Variables Within Homemade Functions