Wolfram Language Fast Introduction for Math Students
Get Started »

Variables & Functions

Variables start with letters and can also contain numbers:

(It’s best to start with lowercase letters, reserving capitals for built-in objects.)
In[1]:=
Click for copyable input
a1/2
Out[1]=

A space between two variables or numbers indicates multiplication:

(In other words, “a b” is a times b, whereas “ab” is the variable ab.)
In[2]:=
Click for copyable input
a b + 5 x x
Out[2]=

Use /. and to make substitutions in an expression:

(The “rule” can be typed as ->.)
In[3]:=
Click for copyable input
1 + 2 x /. x -> 2
Out[3]=

Assign values using the = symbol:

In[1]:=
Click for copyable input
x = 2
Out[1]=

Use your variable in expressions and commands:

In[2]:=
Click for copyable input
1 + 2 x
Out[2]=

Clear the assignment, and x remains unevaluated:

In[3]:=
Click for copyable input
Clear[x]
1 + 2 x
Out[3]=

Define your own functions with the construction f[x_]:=

In[1]:=
Click for copyable input
f[x_] := 1 + 2 x

x_ means that x is a pattern that can have any value substituted for it.

:= means that any argument passed to f is substituted into the right-hand side upon evaluation:

In[2]:=
Click for copyable input
f[2]
Out[2]=

QUICK REFERENCE: Defining Variables and Functions »