3) User defined functions and modules:
Functions
The following defines a function q. Note the underscore in x_ and the := sign.
In[41]:=
Out[42]=
This function can be used like any other function.
In[43]:=
Out[43]=
If you use :=, the right-hand side of the definition is evaluated each time the function is called. This can often be very confusing!
In[44]:=
Out[45]=
Modules
When a procedure is to complicated for a single function, then use a module. Module allows you to set up local variables with names that are local to the module. A module returns the last thing it computes. Modules are analogous to subroutines in FORTRAN.
In[47]:=
In[52]:=
In[56]:=
Out[57]=
Created by Mathematica (June 17, 2004)