Introduction to Programming with Mathematica, Third Edition

4.5: Auxiliary functions

4.5 Auxiliary functions

There are several major drawbacks to the deal function created in the previous section. In order to use deal, the definition of removeRand and the value of cardDeck must be entered before calling deal. It would be much more convenient if we could incorporate these functions within the deal function definition itself. In the next section, we will show how this can be done.

Compound functions

The left-hand side of a compound function is the same as that of any user-defined function. The right-hand side consists of consecutive expressions enclosed in parentheses and separated by semicolons.

    <i class="emphasis">name[arg</i><sub1</sub>_, <i class="emphasis">arg</i><sub2</sub>_,..., <i class="emphasis">arg</i><subn</sub>_] := (<i class="emphasis">expr</i><sub1</sub>; <i class="emphasis">expr</i><sub2</sub>;...; <i class="emphasis">expr<subm</sub></i>)

The expressions can be user-defined functions (also known as auxiliary functions), value declarations, and function calls. When a compound function is evaluated with particular argument values, these expressions are evaluated in order and the result of the evaluation of the last expression is returned (by adding a semicolon after expr n, the display of the final evaluation result can also be suppressed).

We will work with the deal function to illustrate how a compound function is created. We need the following three expressions.

<span class="inlinequation"><span class="inlinemediaobject"><img alt="Image from book"> border="0"> height="27"> id="IMG_104"> src="https://images.books24x7.com/bookimages/id_10817/figu96_1.jpg"> title=""> width="400"></span></span>    <i class="emphasis">In[2]:</i>= <b class="bold">rempveRand[lis_] :=             Delete[lis, Random[Integer, {1, Length[lis]}]]</b>    <i class="emphasis">In[3]</i>:=...

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: DC-DC Converter Chips
Finish!
Privacy Policy

This is embarrasing...

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