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.
SetDelayed 
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.
Details
- SetDelayed has attribute HoldAll, rather than 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 Null if the assignment specified can be performed, and returns $Failed otherwise.
Examples
open all close allBasic Examples (1)
Scope (9)
Left Hand Sides (4)
A variable defined with SetDelayed is evaluated every time it is used:
r := RandomReal[]
{r, r, r}Make definitions for special and general cases using immediate and delayed assignments, respectively:
fact[1] = 1;
fact[n_] := n fact[n - 1]
fact[10]f[x_ /; x > 0] := Sqrt[2x]So f evaluates only when the condition is satisfied:
{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]}Different Kinds of Values (5)
r := RandomReal[]
OwnValues[r]f[x_] := x ^ 2
DownValues[f]derivative[1][f][x_] := fg[x];
SubValues[derivative]mod/:a_mod + b_mod := modPlus[a, b]
UpValues[mod]Numerical values:
N[f[x_]] := Product[1 - x ^ -i, {i, 2, 10}];
NValues[f]Perform a numerical evaluation of f:
N[f[2]]Applications (3)
Define and apply 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]
pi2Definition[pi2]Definitions for unevaluated expressions can implement call-by-name semantics using a held argument:
SetAttributes[f, HoldFirst]
f[sym_, val_] := (sym = val ^ 2)x = 17;Now evaluate f, which sets a new value for x:
f[x, 5]The global variable has indeed been modified:
xProperties & Relations (9)
The right side of an immediate definition is evaluated when the definition is made:
x = RandomReal[];
{x, x, x}In contrast, the right side of a delayed definition is evaluated each time the definition is used:
y := RandomReal[];
{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]A pattern variable is renamed if necessary inside a nested scope:
makedef[z_] := (f[x_] := z ^ 2;)
makedef[x]
Definition[f]So the input is matched to a pattern variable, whereas the variable on the right side of the definition is not matched to it:
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]Use these definitions to evaluate f[10]:
fact[10]Definition prints definitions associated with a symbol:
f[x_] := x ^ 2
Definition[f]Information prints various information about a symbol, including any definitions:
? fDownValues returns a list of rules corresponding to any downvalues defined:
DownValues[f]Give f both a specific and a general definition:
f[-1] = I;
f[x_] := x ^ 2
SetAttributes[f, Listable]Use Unset (=.) to clear definitions with a particular left-hand side:
f[-1]=.Check that only the general definition remains:
Definition[f]Clear any definitions, but not attributes:
Clear[f]
Definition[f]Use ClearAll to clear attributes too:
ClearAll[f]
Definition[f]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]}Expand[(x + 1) ^ 3]Using delayed definitions may have unexpected consequences:
f[x_] := %The definition actually made and its behavior:
Definition[f]Evaluation simply resets to the prior evaluation's result:
f[1]Again evaluate an expression for subsequent use:
Expand[(x + 1) ^ 3]Use Evaluate to force evaluation of the right-hand side:
g[x_] := Evaluate[%]
Definition[g]Check that it evaluates as expected:
g[1]Again evaluate an expression for subsequent use:
Expand[(x + 1) ^ 3]Or use Set to force evaluation of the right-hand side:
h[x_] = %Definition[h]Use it to perform an evaluation:
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]Neat Examples (1)
Dynamic programming for the Fibonacci sequence uses caching to save evaluated values:
fib[1] = fib[2] = 1;
fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]fib[5]New definitions for prior evaluations have been added during the calculation:
Definition[fib]Tech Notes
Related Guides
Related Workflows
- Clear Definitions for Symbols and Functions ▪
- Find All Defined Functions
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.
CMS
Wolfram Language. 1988. "SetDelayed." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetDelayed.html.
APA
Wolfram Language. (1988). SetDelayed. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetDelayed.html
BibTeX
@misc{reference.wolfram_2026_setdelayed, author="Wolfram Research", title="{SetDelayed}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/SetDelayed.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_setdelayed, organization={Wolfram Research}, title={SetDelayed}, year={1988}, url={https://reference.wolfram.com/language/ref/SetDelayed.html}, note=[Accessed: 13-June-2026]}