Message Boards Message Boards

What's the difference between f[x_]= ... and f[x_]:=...

Posted 9 years ago

thanks

POSTED BY: erivaldo nunes
2 Replies

RandomInteger[10] is a command that produces a random number from 0 to 10.

  • Evaluate RandomInteger[10] a bunch of times till you have a feel for what it does.
  • Now evaluate this command:

    myRandomNumber = RandomInteger[10]
    

Play around with myRandomNumber. How does myRandomNumber behave? myRandomNumber is always the same number. The right hand side of the equal sign was evaluated once and became the value for myRandomNumber.

  • Now evaluate this command:

    myOtherRandomNumber := RandomInteger[10]
    

Play around with myOtherRandomNumber. How does it behave? myOtherRandomNumber is always a different number. The right hand side of the equal sign re-evaluates every time you call myOtherRandomNumber.

POSTED BY: Sean Clarke

yes there is, the := notation reevaluates the right-hand-side each time it is evaluated. So there is a difference between these two definitions:

ClearAll[p,f]
p=2;
f[x_]:=x+p;
p=3;
f[2]


ClearAll[p,f]
p=2;
f[x_]=x+p;
p=3;
f[2]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract