Wolfram Language Fast Introduction for Programmers
Get Started »

Function Definitions

In the Wolfram Language, function definitions are just assignments that give transformation rules for patterns.

Unlike methods in the object-oriented Java language, Wolfram Language functions are not restricted to a class hierarchy.

Note that in the Wolfram Language, parameter names are followed by an underscore when declaring a function, unlike in Python. If you omit the underscore in the Wolfram Language, the symbol is treated literally, which can be useful when you want to pattern-match your arguments for specific inputs.

Define a function of two arguments named x and y:

In[1]:=
X

In basic Wolfram Language function definitions, parameter names are followed by _ ("blank"). Unlike Java methods, Wolfram Language functions will accept any type of data by default.

Use the definition:

In[2]:=
X
Out[2]=

Clear the definition:

In[3]:=
X

Functions can be defined by specifying their values in a sequence of cases:

In[1]:=
X
Out[1]=
In[2]:=
X
Out[2]=

Any undefined case is left untransformed:

In[3]:=
X
Out[3]=

You can use /; to restrict a definition to apply only when a particular condition holds:

In[4]:=
X

Using /; in the Wolfram Language is like a shortened If statement.

The /; operator works like a shortened If statement in the Wolfram Language.

You can define transformations for any left-hand side—a convenient way to destructure arguments:

In[1]:=
X
In[2]:=
X
Out[2]=

The Wolfram Language doesnt have types as such; everything is a symbolic expression. The head of an expression can be used as a kind of generalized type designator.

Give different definitions for what f should do when applied to a u object or a v object:

In[1]:=
X
In[2]:=
X

This is similar to overloading a Java method for different data types, except that Wolfram Language functions can be overloaded more generally, based on the symbolic structure of their arguments.

A Wolfram Language function can have flexible definitions, its implementation changing depending on the symbolic structure of its arguments. This sort of functionality is similar to single-dispatch generic functions from Python 3's functools module.

QUICK REFERENCE: Defining Variables and Functions »

Check Your Understanding

Which of the following defines a function f that adds two numbers (e.g., f[3, 4] == 7)?

f[x + y]

Incorrect. This does not define a function.

f[x_, y_] -> x + y

Incorrect. This is a rule, not a function definition.

f[x_, y_] := x + y

Correct.

f[x, y] := x + y

Incorrect. This function only matches the symbols x and y, and will do nothing when given numbers.

Which definition would make {f[1], f[2], f[3], f[4]} evaluate to {f[1], 5, 6, f[4]}?

f[x_] = x

Incorrect. With this definition, the result would be {1, 2, 3, 4}.

f[x_] := x + 3 /; 2 <= x <= 3

Correct.

f[x_] := x + 3

Incorrect. This definition would also make f[1] and f[4] evaluate.

If the function f is defined as f[x_, y_] := x + y, then which of these is the value of f[a, b]?

x + y

Incorrect. The arguments a and b replace the occurences of x and y.

f[a, b]

Incorrect. The patterns x_ and y_ match any expression, including a and b.

a + b

Correct.