Functions and Programs
Defining Functions | Repetitive Operations |
Functions as Procedures | Transformation Rules for Functions |
Manipulating Options |
There are many functions that are built into the Wolfram Language. This tutorial discusses how you can add your own simple functions to the Wolfram Language.
As a first example, consider adding a function called f which squares its argument. The Wolfram Language command to define this function is f[x_]:=x^2. The _ (referred to as "blank") on the left‐hand side is very important; what it means is discussed here. For now, just remember to put a _ on the left‐hand side, but not on the right‐hand side, of your definition.
f[x_]:=x^2 | define the function f |
?f | show the definition of f |
Clear[f] | clear all definitions for f |
The names like f that you use for functions in the Wolfram Language are just symbols. Because of this, you should make sure to avoid using names that begin with capital letters, to prevent confusion with built‐in Wolfram Language functions. You should also make sure that you have not used the names for anything else earlier in your session.
When you have finished with a particular function, it is always a good idea to clear definitions you have made for it. If you do not do this, then you will run into trouble if you try to use the same function for a different purpose later in your Wolfram Language session. You can clear all definitions you have made for a function or symbol f by using Clear[f].
In many kinds of calculations, you may find yourself typing the same input to the Wolfram Language over and over again. You can save yourself a lot of typing by defining a function that contains your input commands.
The functions you define in the Wolfram Language are essentially procedures that execute the commands you give. You can have several steps in your procedures, separated by semicolons.
The result you get from the whole function is simply the last expression in the procedure. Notice that you have to put parentheses around the procedure when you define it like this:
expr1;expr2;… | a sequence of expressions to evaluate |
Module[{a,b,…},proc] | a procedure with local variables a, b, … |
When you write procedures in the Wolfram Language, it is usually a good idea to make variables you use inside the procedures local, so that they do not interfere with things outside the procedures. You can do this by setting up your procedures as modules, in which you give a list of variables to be treated as local.
The function cex defined above is not a module, so the value of t "escapes", and exists even after the function returns:
There are a number of functions built into the Wolfram Language which, like Plot, have various options you can set. The Wolfram Language provides some general mechanisms for handling such options.
If you do not give a specific setting for an option to a function like Plot, then the Wolfram Language will automatically use a default value for the option. The function Options[function,option] allows you to find out the default value for a particular option. You can reset the default using SetOptions[function,option->value]. Note that if you do this, the default value you have given will stay until you explicitly change it.
Options[function] | give a list of the current default settings for all options |
Options[function,option] | give the default setting for a particular option |
SetOptions[function,option->value,…] | reset defaults |
This resets the default for the PlotRange option. The semicolon stops the Wolfram Language from printing out the rather long list of options for Plot:
The graphics objects that you get from Plot or Show store information on the options they use. You can get this information by applying the Options function to these graphics objects.
Options[plot] | show all the options used for a particular plot |
Options[plot,option] | show the setting for a specific option |
AbsoluteOptions[plot,option] |
While it is often convenient to use a variable to represent a graphic as in the above examples, the graphic itself can be evaluated directly. The typical ways to do this in the notebook interface are to copy and paste the graphic or to simply begin typing in the graphical output cell, at which point the output cell will be converted into a new input cell.
When a plot created with no explicit ImageSize is placed into an input cell, it will automatically shrink to more easily accommodate input.
The following input cell was created by copying and pasting the graphical output created in the previous example:
Restore Plot to its default option settings:
In using the Wolfram Language, you sometimes need to repeat an operation many times. There are many ways to do this. Often the most natural is in fact to set up a structure such as a list with many elements, and then apply your operation to each of the elements.
Another approach is to use the Wolfram Language function Do, which works much like the iteration constructs in languages such as C and Fortran. Do uses the same Wolfram Language iterator notation as Sum and Product, described in "Sums and Products".
Do[expr,{i,imax}] | evaluate expr with i running from 1 to imax |
Do[expr,{i,imin,imax,di}] | evaluate expr with i running from imin to imax in steps of di |
Print[expr] | print expr |
Table[expr,{i,imax}] | make a list of the values of expr with i running from 1 to imax |
If you do not give an iteration variable, the Wolfram Language simply repeats the operation you have specified, without changing anything:
"Values for Symbols" discussed how you can use transformation rules of the form x->value to replace symbols by values. The notion of transformation rules in the Wolfram System is, however, quite general. You can set up transformation rules not only for symbols, but for any Wolfram System expression.
Probably the most powerful aspect of transformation rules in the Wolfram System is that they can involve not only literal expressions, but also patterns. A pattern is an expression such as f[t_] which contains a blank (underscore). The blank can stand for any expression. Thus, a transformation rule for f[t_] specifies how the function f with any argument should be transformed. Notice that, in contrast, a transformation rule for f[x] without a blank, specifies only how the literal expression f[x] should be transformed, and does not, for example, say anything about the transformation of f[y].
When you give a function definition such as f[t_]:=t^2, all you are doing is telling the Wolfram System to automatically apply the transformation rule f[t_]->t^2 whenever possible.
"Patterns" and "Transformation Rules and Definitions" explain in detail how to set up patterns and transformation rules for any kind of expression. Suffice it to say here that in the Wolfram System all expressions have a definite symbolic structure; transformation rules allow you to transform parts of that structure.