Introduction to Programming with Mathematica, Third Edition

Chapter 4: Functional Programming

Programming in Mathematica is essentially a matter of writing user-defined functions that work like mathematical functions; when applied to specific values, they perform computations producing results. In fact, these functions can operate on arbitrary expressions, including other functions. This functional style of programming distinguishes Mathematica from more traditional procedural languages like C and Fortran, and a facility at functional programming is essential for taking full advantage of Mathematica's powerful language to solve your computational tasks.

4.1 Introduction

Functions are objects that operate on expressions and output unique expressions for each input. We can think of functions as mathematicians do. For example, here is a definition for a function of two variables.

    <i class="emphasis">In[1]</i>:= <b class="bold">f [x_, y_] :=Cos [x] +Sin[y]</b>

You can evaluate the function for numeric or symbolic values.

    <i class="emphasis">In[2]</i>:= <b class="bold">f[<span class="unicode">?</span>, 1.6]</b>    <i class="emphasis">Out[2]</i>= - 0.000426397    <i class="emphasis">In[3]</i>:= <b class="bold">f [<span class="unicode">?</span> <span class="unicode">?</span>]</b>    <i class="emphasis">Out[3]</i>= Cos [<span class="unicode">?</span>] + Sin[<span class="unicode">?</span>]

Functions can be significantly more complicated objects. Below is a function that operates on functions. Like the function f above it takes two arguments, but, in this case, its arguments are a function or expression, and a list containing the variable of integration and the integration limits.

This particular function can be also be called with a function and a variable.

    <i class="emphasis">In[5]</i>:= <b class="bold">Integrate[Exp [I <span class="unicode">?</span>

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: Waveplates and Retardation Plates
Finish!
Privacy Policy

This is embarrasing...

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