Back: Programming
Previous: Pattern Matching; using Head and tests
Next: Compound expressions, conditionals

Programming in Mathematica

Syntax of   ==   vs.   :=   vs.   =

==   is used to indicate an equation or equality.

lhs == rhs   evaluates to either True or False.

Most often, the == is used in commands like Solve,
e.g.,

Solve[ x2 - 5x + 6 == 0,   x ]
=   vs.   :=

Both of these symbols are used to assign values, e.g.,

area = Pi r^2
area := Pi r^2

Both set the value of symbol ``area'';
the first evaluates it immediately (using the current value of r),
while the second uses delayed evaluation -- waiting until area is actually used before
doing a calculation with r.

The difference between the two is illustrated by the sample commands below



c = 2;                           (* define f and g in terms of c *)
f[x_]  = c x^2;
g[x_] := c x^2;

{f[z], g[z], f[5], g[5]}         (* so far, f and g are the same *)

	    2     2
	{2 z , 2 z ,  50, 50}


c = 3;                           (* change c *)
{f[z], g[z], f[5], g[5]}         (* now f and g are different *)

	    2     2
	{2 z , 3 z ,  50, 75}