Re: Using "=" vs ":=" [Thanks!]

• To: mathgroup at smc.vnet.net
• Subject: [mg70715] Re: Using "=" vs ":=" [Thanks!]
• From: "." <ggroup at sarj.ca>
• Date: Tue, 24 Oct 2006 02:24:22 -0400 (EDT)
• References: <ehf1ks\$5ds\$1@smc.vnet.net><ehhqv3\$7hh\$1@smc.vnet.net>

```As some other responses mentioned, it depends on when you want
evaluation to occur.  Set (=) is useful when you want to store and
refer to the *output* of some code.  SetDelayed (:=) is useful when you
want to store and run the code itself.  In many cases, Mathematica may
return the code itself, so the difference between set and setdelayed
are not as clear.

One example of where Set may be better is when you have some long or
complex calculation that can be run once and yet use the result many
times.  An example of when SetDelayed may be better is when your code
includes some portion that might change with each evaluation, like
random number generation or Print statements.

A relatively simple, but illustrative, example would be to have
functions g1 and g2 which multiply their argument by a random number:
In[1]:=
g1[x_] := x*Random[]
g2[x_] = x*Random[]
Out[2]=
0.272609 x

Now if we run each of these 5 times, see what happens:
In[3]:=
Table[g1[x],{5}]
Out[3]=
{0.98188 x, 0.870939 x, 0.978272 x, 0.122654 x, 0.502047 x}

In[4]:=
Table[g2[x],{5}]
Out[4]=
{0.272609 x, 0.272609 x, 0.272609 x, 0.272609 x, 0.272609 x}

So as you can see, you get very different results using Set vs.
SetDelayed.  These are by no means comprehensive examples or
explanations, but hopefully they further your understanding.

misha wrote:
> Thanks to all who responded.
>
> Lesson 1: Caution for inexperienced users: Arguments of functions must
> be contained in brackets, "[...]", not parantheses, "(...)".  This is
> not mere notation!
> Lesson 2: Set (=) vs. SetDelayed (:=) can be important.  I'm still
> trying to figure out when.  The examples in the help files and some of
> the responses have helped, but I'm trying to still trying to grasp the
> more general concept.
>
> Thanks again!
>
> Misha
>
> misha wrote:
> > I'm going through Mathematic by Example, 2nd ed., (Abell and Braselton),
> > and have come across something that puzzles me.
> >
> > Chapter 2, Section 2, Example 8
> > Define f(x,y)=1-sin(x^2+y^2)
> >
> > So I first try,
> > In[1]:= f[x_, y_]:=1-Sin(x^2+y^2)
> > No problem so far...
> > Then,
> > In[2]:= f[x,y]
> > Out[2]:=1-Sin(x^2+y^2)
> > Still no problem...
> > Then,
> > In[3]:=f[1,2]
> > Out[3]:=1-5 Sin
> >
> > Huh?
> >
> > I noticed that rather than using ":=" to "simply define" this function,
> > as opposed to (just) "=" to "define and compute" this function, I get
> > different subsequent behavior.  Specifically, doing the above with just
> > "=", works fine.
> > In[1]:= f[x_, y_]=1-Sin(x^2+y^2)
> > ....
> > In[3]:=f[1,2]
> > Out[3]:=1-Sin[5]
> >
> > My question is, Why?  What's the difference between ":=" and "=" for
> > defining functions?
> >
> > Thanks!
> > Misha
> >

```

• Prev by Date: Re: question ,,thankx!!!
• Next by Date: new procedure for converting a new recursive polynomial set into matrices
• Previous by thread: RE: Re: Using "=" vs ":=" [Thanks!]
• Next by thread: two questions