Using Mathematica » Basics

Functions

Functions are common to all programming languages. They work exactly like the name suggests. A single input or set of inputs is mapped to a single output.

In Mathematica, functions are defined using variables followed by an underscore square brackets and can be defined with either = or := , which has the same meaning as with variables.

= uses the immediate values of the right hand side while := recalculates these when called for.

The := definition works most similarly to standard programming languages and is the definition I would suggest using by default

Here we'll define a simple function to add two primes using :=

 primeSum[n1_,n2_]:=Prime[n1]+Prime[n2]
primeSum[1,10]
 31

The same works with plain =

 primeSum[n1_,n2_]=Prime[n1]+Prime[n2];
primeSum[1,10]
 31

Differences between = and :=

Let's write a more complex function that moves a position around randomly. This position could be the position of an atom, say as hydrogen. Let's start it out at the origin.

 hydrogenPosition={0,0,0};

Now we'll define a function to move our hydrogen around randomly and return where it is now.

 moveOurH[]:=
  Row@{
    "H is at:",
    hydrogenPosition+=
      {
        RandomReal[{-1,1}],
        RandomReal[{-1,1}],
        RandomReal[{-1,1}]
        }
    }
moveOurH[]
 Row[{"H is at:", {-0.839815, 0.504928, -0.876682}}]

We can move it again and again, and it'll randomly walk around:

 moveOurH[]
moveOurH[]
moveOurH[]
 Row[{"H is at:", {-0.523173, -0.378415, -0.278032}}]
 Row[{"H is at:", {-0.464874, 0.338277, -0.101727}}]
 Row[{"H is at:", {-0.973597, 0.327556, -0.682705}}]

Now let's try to do the same, but using =

 hydrogenPosition={0,0,0};
moveOurH[]=
  Row@{
    "H is at:",
    hydrogenPosition+=
      {
        RandomReal[{-1,1}],
        RandomReal[{-1,1}],
        RandomReal[{-1,1}]
        }
    };
moveOurH[]
moveOurH[]
moveOurH[]
 Row[{"H is at:", {-0.939441, 0.210649, -0.231079}}]
 Row[{"H is at:", {-0.939441, 0.210649, -0.231079}}]
 Row[{"H is at:", {-0.939441, 0.210649, -0.231079}}]

You'll notice our position doesn't change. That's because the right hand side was evaluated as the function was defined.

Note that this is not always a bug. Sometimes this can be a feature as there are instances where having most values set is useful.

Function

There is also a special object called a Function which is defined as follows:

 Function[{var1,var2,var3,...,varn}, code]

This is useful primarily in complicated code when a simple function needs to be used just once or twice and it is not worth defining a full function to do this.

This form of function can also be defined as follows:

 (code&)

where var1,var2,var3,...,varn are replaced by ``````#1,#2,...,#n in the code. For example:

 Function[
  {a,b,c},
  {
    {"a is:",a},
    {"b is:",b},
    {"c is:",c}
    }
  ]

could be written as:

 ({
  {"a is:",#1},
  {"b is:",#2},
  {"c is:",#3}
  }&)

And to see that they are in fact the same function. We call the first:

 Function[
  {a,b,c},
  {
    {"a is:",a},
    {"b is:",b},
    {"c is:",c}
    }
  ][1,2,3]
 {{"a is:",1},{"b is:",2},{"c is:",3}}

And then the second:

 ({
  {"a is:",#1},
  {"b is:",#2},
  {"c is:",#3}
  }&)[1,2,3]
 {{"a is:",1},{"b is:",2},{"c is:",3}}

And note that they give exactly the same output.

In Mathematica these are known as pure functions, to distinguish them from functions defined the way we've done before.