({{, lhs:=rhs
assigns rhs to be the delayed value of lhs. rhs is maintained in an unevaluated form. When lhs appears, it is replaced by rhs, evaluated afresh each time. }})

OpenersNotesSection(Spacer1)Details

(RefLink[SetDelayed, paclet:ref/SetDelayed]) has attribute (RefLink[HoldAll, paclet:ref/HoldAll]), rather than (RefLink[HoldFirst, paclet:ref/HoldFirst]).

You can make assignments of the form lhs:=rhs/;test, where test gives conditions for the applicability of each transformation rule. You can make several assignments with the same lhs but different forms of test.

lhs:=rhs returns (RefLink[Null, paclet:ref/Null]) if the assignment specified can be performed, and returns (RefLink[$Failed, paclet:ref/$Failed]) otherwise.

OpenersPrimaryExamplesSection(Spacer1)Examples  (27)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

f[x_]:=Expand[x^2]

f[a+b]

OpenersExampleSection1(Spacer1)Scope  (9)

OpenersExampleSubsection0(Spacer1)Left Hand Sides  (4)

A variable defined with (RefLink[SetDelayed, paclet:ref/SetDelayed]) is evaluated every time it is used:

r:=Random[]

{r,r,r}

Make definitions for special and general cases using immediate and delayed assignments:

fact[1]=1;
fact[n_]:=n fact[n-1]

fact[10]

Make conditional definitions:

f[x_/;x>0]:=Sqrt[2x]

{f[2],f[-2]}

Define a function by several conditional cases:

unit[x_/;x<0]:=0
unit[x_/;x≥0]:=1

{unit[-2],unit[0],unit[1],unit[a]}

OpenersExampleSubsection1(Spacer1)Different Kinds of Values  (5)

Ownvalues:

r:=Random[]

OwnValues[r]

Downvalues:

f[x_]:=x^2

DownValues[f]

Subvalues:

derivative[1][f][x_]:=fg[x];

SubValues[derivative]

Upvalues:

mod/:a_mod+b_mod:=modPlus[a,b]

UpValues[mod]

Numerical values:

N[f[x_]]:=Product[1-x^-i,{i,2,10}];

NValues[f]

N[f[2]]

OpenersExampleSection2(Spacer1)Applications  (3)

Define a procedure that computes a square root with Newton's method:

newton[r_]:=FixedPoint[Function[x,(x+r/x)/2],r]

newton[5.0]

Perform a calculation on demand and cache the result:

pi2:=pi2=N[Pi^2,50]

pi2

Definition[pi2]

Definitions for unevaluated expressions can implement call-by-name semantics:

SetAttributes[f,HoldFirst]

f[sym_,val_]:=(sym=val^2)

x=17;

f[x,5]

The global variable has been modified:

x

OpenersExampleSection3(Spacer1)Properties & Relations  (9)

The right side of an immediate definition is evaluated when the definition is made:

x=Random[];

{x,x,x}

The right side of a delayed definition is evaluated each time the definition is used:

y:=Random[];

{y,y,y}

The arguments of the left side of a definition are evaluated before the definition is made:

x=5;

f[x]:=17

Definition[f]

Definitions with the same left side overwrite earlier ones:

f[x_]:=x^2

f[x_]:=x^3

f[2]

The pattern variable is renamed if necessary inside a nested scope:

makedef[z_]:=(f[x_]:=z^2;)

makedef[x]

Definition[f]

f[5]

Delayed assignment introduces a scope that is not affected by global variables:

x=5;

f[x_]:=x^2

{f[2],x}

Immediate assignment does not introduce a scope:

g[x_]=x^2;

{g[2],x}

Use a rule to do a transformation to a particular expression:

f[2 x y]+f[x y]//.f[a_ b_]:>f[a]+f[b]

Use a definition to do a transformation automatically for all expressions involving g:

g[a_ b_]:=g[a]+g[b]

g[2 x y]+g[x y]+f[x y]

More specific definitions are put in front of more general ones:

fact[n_]:=n fact[n-1];
fact[1]=1;

Definition[fact]

fact[10]

(RefLink[Definition, paclet:ref/Definition]) prints definitions associated with a symbol:

f[x_]:=x^2

Definition[f]

(RefLink[Information, paclet:ref/Information]) prints various information about a symbol, including any definitions:

?f

(RefLink[DownValues, paclet:ref/DownValues]) returns a list of rules corresponding to any downvalues defined:

DownValues[f]

f[-1]=I;
f[x_]:=x^2
SetAttributes[f,Listable]

Use (RefLink[Unset, paclet:ref/Unset]) (=.) to clear definitions with a particular left-hand side:

f[-1]=.

Definition[f]

Clear any definitions, but not attributes:

Clear[f]

Definition[f]

Use (RefLink[ClearAll, paclet:ref/ClearAll]) to clear attributes, too:

ClearAll[f]

Definition[f]

OpenersExampleSection4(Spacer1)Possible Issues  (4)

Omitting the _ defines a transformation only for a literal value, rather than a function:

f[x]:=x^2;
g[x_]:=x^2;

{f[x],g[x]}

{f[2],g[2]}

Using delayed definitions may have unexpected consequences:

Expand[(x+1)^3]

f[x_]:=%

The definition actually made and its behavior:

Definition[f]

f[1]

Use (RefLink[Evaluate, paclet:ref/Evaluate]) to force evaluation of the right-hand side:

Expand[(x+1)^3]

g[x_]:=Evaluate[%]

Definition[g]

g[1]

Or use (RefLink[Set, paclet:ref/Set]) to force evaluation of the right-hand side:

Expand[(x+1)^3]

h[x_]=%

Definition[h]

h[1]

An iterative runaway definition:

f[x_]:=f[x+1]

f[1]

By providing a base case, the iteration terminates:

g[10]=10;
g[x_]:=g[x+1]

g[1]

A recursive runaway definition:

f[x_]:=f[Floor[x/2]]+1

f[10]

By providing a base case, the recursion terminates:

g[1]=0;
g[x_]:=g[Floor[x/2]]+1

g[10]

OpenersExampleSection5(Spacer1)Neat Examples  (1)

Dynamic programming for the Fibonacci sequence:

fib[1]=fib[2]=1;
fib[n_]:=fib[n]=fib[n-1]+fib[n-2]

fib[5]

New definitions have been added during the calculation:

Definition[fib]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Set, paclet:ref/Set])  ▪  (RefLink[TagSetDelayed, paclet:ref/TagSetDelayed])  ▪  (RefLink[Unset, paclet:ref/Unset])  ▪  (RefLink[Clear, paclet:ref/Clear])  ▪  (RefLink[RuleDelayed, paclet:ref/RuleDelayed])  ▪  (RefLink[Dynamic, paclet:ref/Dynamic])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Immediate and Delayed Definitions, paclet:tutorial/ImmediateAndDelayedDefinitions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Defining Variables and Functions, paclet:guide/VariablesAndFunctions])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}, {▪(RefLinkPlain[Wolfram Language Syntax, paclet:guide/Syntax])}, {▪(RefLinkPlain[Rules & Patterns, paclet:guide/RulesAndPatterns])}, {▪(RefLinkPlain[Assignments, paclet:guide/Assignments])}, {▪(RefLinkPlain[Scoping Constructs, paclet:guide/ScopingConstructs])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Assignments, http://www.wolfram.com/language/fast-introduction-for-programmers/assignments/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Function Definitions, http://www.wolfram.com/language/fast-introduction-for-programmers/function-definitions/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/39-immediate-and-delayed-values.html])(WebLink[: Immediate and Delayed Values, https://www.wolfram.com/language/elementary-introduction/39-immediate-and-delayed-values.html])}})}})