**1.11.11 ****MathLink**

The previous section discussed how to exchange plain text with external programs. In many cases, however, you will find it convenient to communicate with external programs at a higher level, and to exchange more structured data with them.

On almost all computer systems, Mathematica supports the MathLink communication standard, which allows higher-level communication between Mathematica and external programs. In order to use MathLink, an external program has to include some special source code, which is usually distributed with Mathematica.

MathLink allows external programs both to call Mathematica, and to be called by Mathematica. Section 2.12 discusses some of the details of MathLink. By using MathLink, you can, for example, treat Mathematica essentially like a subroutine embedded inside an external program. Or you can create a front end that implements your own user interface, and communicates with the Mathematica kernel via MathLink.

You can also use MathLink to let Mathematica call individual functions inside an external program. As described in Section 2.12, you can set up a MathLink template file to specify how particular functions in Mathematica should call functions inside your external program. From the MathLink template file, you can generate source code to include in your program. Then when you start your program, the appropriate Mathematica definitions are automatically made, and when you call a particular Mathematica function, code in your external program is executed.

Calling functions in external programs.

This starts the external program simul, and installs Mathematica definitions to call various functions in it.
In[1]:= **Install["simul"]**

Out[1]=

Here is a usage message for a function that was installed in Mathematica to call a function in the external program.
In[2]:= **?srun**

srun[{a, r, gamma}, x] performs a simulation with the specified parameters.

When you call this function, it executes code in the external program.
In[3]:= **srun[{3, 0, 7}, 5]**

Out[3]=

This terminates the simul program.
In[4]:= **Uninstall["simul"]**

Out[4]=