lhs=rhs
evaluates rhs and assigns the result to be the value of lhs. From then on, lhs is replaced by rhs whenever it appears.
{l1,l2,…}={r1,r2,…}
evaluates the ri, and assigns the results to be the values of the corresponding li.
Set 
lhs=rhs
evaluates rhs and assigns the result to be the value of lhs. From then on, lhs is replaced by rhs whenever it appears.
{l1,l2,…}={r1,r2,…}
evaluates the ri, and assigns the results to be the values of the corresponding li.
Details
- lhs can be any expression, including a pattern.
- f[x_]=x^2 is a typical assignment for a pattern. Notice the presence of _ on the left‐hand side, but not the right‐hand side.
- An assignment of the form f[args]=rhs sets up a transformation rule associated with the symbol f.
- Different rules associated with a particular symbol are usually placed in the order that you give them. If a new rule that you give is determined to be more specific than existing rules, it is, however, placed before them. When the rules are used, they are tested in order. »
- New assignments with identical lhs overwrite old ones. »
- You can see all the assignments associated with a symbol f using ?f or Definition[f].
- If you make assignments for functions that have attributes like Flat and Orderless, you must make sure to set these attributes before you make assignments for the functions.
- Set has attribute HoldFirst.
- If lhs is of the form f[args], then args are evaluated. »
- There are some special functions for which an assignment to s[f[args]] is automatically associated with f rather than s. These functions include: Attributes, Default, Format, MessageName, Messages, N, and Options. »
- When it appears in an unevaluated symbolic form, Set is treated as a scoping construct so that variables in nested occurrences are renamed if necessary. »
- lhs=rhs returns rhs even if for some reason the assignment specified cannot be performed.
- Some global variables such as $RecursionLimit can only be assigned a certain range or class of values.
Background & Context
- Set is a function that evaluates and assigns an expression to be the value of a variable. The expression
is commonly represented using the shorthand syntax
. After Set is evaluated, lhs is replaced by rhs whenever it appears. Depending on the form of lhs, the result is stored as in the associated OwnValues, DownValues, or a specialized data structure. - Set often contains patterns on the lhs that are assigned transformed values as indicated on the rhs, e.g.
.
evaluates rhs immediately, so in situations where rhs should be evaluated only after the assignment is made, SetDelayed (written in shorthand as :=) should be used instead. - The assignments associated with a given symbol can be seen using Definition[f]. Individual assignments may be removed from a symbol using Unset; Clear and ClearAll remove all definitions at once.
Examples
open all close allBasic Examples (2)
Scope (15)
Left-Hand Sides (7)
i = 1;
While[Prime[i] < 100, i = i + 1];iSet values for "indexed variables":
a[1] = x;a[2] = y;{a[1], a[2], a[3]}Define a function from an expression:
Expand[(1 + x) ^ 3]f[x_] = %f[a + b]Use Block to temporarily set variables:
Block[{$RecursionLimit = 20}, x = x + 1]v = {a, b, c, d}v[[2]] = xvv = 1 + x ^ 5v[[2, 2]] = 77777vmat = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |);mat[[2]] = mat[[2]] + 10;
MatrixForm[mat]mat[[All, 3]] = {100, 101, 102};
MatrixForm[mat]Different Kinds of Values (8)
x = 7;OwnValues[x]a[1] = 17;DownValues[a]derivative[1][f] = fg;SubValues[derivative]sq/:area[sq] = s ^ 2;UpValues[sq]Default[f] = 0;Options[f] = {opt1 -> def1, opt2 -> def2};DefaultValues[f]N[const] = Product[1 - 2 ^ -i, {i, 2, 10}];NValues[const]N[const]Format[a] = α;FormatValues[a]aA definition for Attributes is associated with
, rather than Attributes:
Attributes[f] = HoldAllDefinition[f]Generalizations & Extensions (5)
x = y = 77{x, y}{x, y} = {a, b}{x, y} = {y, x}{x, y}v = SparseArray[{1 -> 1, 4 -> 4}]v[[2]] = 3v is still a sparse array, with its second part changed:
{v, Normal[v]}Assign multiple return values of a function to individual variables:
{time, res} = Timing[N[Catalan, 100000]];{time, N[res]}A definition for a pattern with a specific head is associated with that head:
_a = αDefinition[a]{a, a[1]}Applications (6)
Compute the GCD of two numbers:
{a, b} = {27, 6};
While[b ≠ 0, {a, b} = {b, Mod[a, b]}];
ax = 1.0;
While[Cos[x] ≠ x, x = Cos[x]];
xCompute
using Newton's method:
x = 1.0;
Do[x = (x + 2 / x) / 2, {5}];
xUse a variable as an abbreviation for a complicated expression used more than once:
x = (a + b)^2;1 + x + 1 / xA scalar implementation of backsubstitution in a system of linear equations:
upperTriangularLinearSolve[ U_, v_ ] :=
Module[ {x, m, n},
{m, n} = Dimensions[U]; x = Range[n];
Do[x[[i]] = (v[[i]] - Subsuperscript[∑, j = i + 1, n]U[[i, j]] x[[j]]) / U[[i, i]], {i, n, 1, -1}];
x
]upperTriangularLinearSolve[{{1, 2}, {0, 3}}, {1, 2} ]A vector implementation of backsubstitution in a system of linear equations:
upperTriangularLinearSolve[ U_, v_ ] :=
Module[ {x, m, n},
{m, n} = Dimensions[U]; x = Range[n];
Do[x[[i]] = (v[[i]] - U[[i, i + 1 ;; n]].x[[i + 1 ;; n]]) / U[[i, i]], {i, n, 1, -1}];
x
];upperTriangularLinearSolve[{{1, 2}, {0, 3}}, {1, 2} ]Properties & Relations (9)
The right side of an immediate definition is evaluated when the definition is made:
x = RandomReal[];Multiple copies are the same value:
{x, x, x}The right side of a delayed definition is evaluated each time the definition is used:
y := RandomReal[];Now it evaluates three different times:
{y, y, y}The arguments of the left side of a definition are evaluated before the definition is made:
x = 5;f[x] = 17Definition[f]Definitions with the same left side overwrite earlier ones:
a = 5;a = 6;See that it has the new value:
aMake definitions for special and general cases using immediate and delayed assignments:
fact[1] = 1;
fact[n_] := n fact[n - 1]
fact[10]More specific definitions are put in front of more general ones:
fact[n_] := n fact[n - 1];
fact[1] = 1;
Definition[fact]fact[10]The pattern variable is renamed if necessary inside a nested scope:
makedef[z_] := (f[x_] = z ^ 2;)
makedef[x]Definition[f]f[5]Module introduces new symbols, distinct from global ones:
x = 5;
Module[{x}, x = 17;x]Check that the global value of x is retained:
xa = Sqrt[2]Definition prints definitions associated with a symbol:
Definition[a]Information prints various information about a symbol, including any definitions:
? aOwnValues returns a list of rules corresponding to any downvalues defined:
OwnValues[a]Define a function fact to compute factorials:
fact[1] = 1;
fact[n_] := n fact[n - 1]Use Unset (=.) to clear definitions with a particular left-hand side:
fact[1]=.Check that the cleared definition is no longer present:
Definition[fact]Clear[fact]Definition[fact]Possible Issues (4)
In the presence of global variables, pattern variables may show unexpected behavior:
x = 5;Using a non-delayed assignment means the right side is evaluated before Set is done:
f[x_] = x ^ 2;So the evaluation uses the global setting and not the actual input to f:
f[2]Delayed assignments behave as expected:
g[x_] := x ^ 2Check that the value 2 is used and not the global value of 5:
g[2]x = x + 1Setting an initial value avoids this:
y = 5;
y = y + 1Pattern variables are not symbols; you cannot normally assign to them:
wrong[x_] := (x = x ^ 2)
wrong[5]Use local variables for this purpose:
right[x0_] := Module[{x}, x = x0 ^ 2]
right[5]Use upvalues to assign to subscripted variables:
a/:Subscript[a, 1] = 5;
FullForm[UpValues[a]]Subscripted variables are distinct from indexed variables:
a[1] = 6;
FullForm[DownValues[a]]Neat Examples (1)
Compute the arithmetic-geometric mean of two numbers [more info]:
{x, y} = N[{1, 2}, 20];
While[x ≠ y, {x, y} = {(x + y/2), Sqrt[x y]}];xHistory
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), Set, Wolfram Language function, https://reference.wolfram.com/language/ref/Set.html.
CMS
Wolfram Language. 1988. "Set." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Set.html.
APA
Wolfram Language. (1988). Set. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Set.html
BibTeX
@misc{reference.wolfram_2026_set, author="Wolfram Research", title="{Set}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Set.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_set, organization={Wolfram Research}, title={Set}, year={1988}, url={https://reference.wolfram.com/language/ref/Set.html}, note=[Accessed: 13-June-2026]}