Mathematica Demystified

Chapter 5: Functions

Even though Mathematica comes with thousands of predefined functions, it is extremely important to know how to define our own functions. While a simple function might be defined in a single line of code, more complicated functions might take dozens of lines assembled together in a Module. In this chapter we'll see how to define our own functions. We'll also learn the basics of elementary programming which can be used to turn any mathematical algorithm into a working Mathematica function.

5.1 Defining Functions

Sometimes we may be using the same function over and over again and it can be handy to give the function a name. For example, suppose we were computing things related to home mortgages and repeatedly found ourselves computing the monthly payment on a loan of $400 000 amortized over 30 years at an annual interest rate of r. Imagine that we keep changing the interest rate r and have to keep retyping the formula for the monthly payment. To save on all that typing, we can create our own function to compute the payment as seen in the following example.

Example 5.1.1
<span class="beginpage"> pagenum="114"><a name="287"></a><a name="page114"></a></span> In[56]:=(* defining our own function *)         (* the monthly payment on a $400000 loan          over 30 years at interest rate r *)

On the left-hand side of the definition we have named the function payment

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Billing Software
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.