The examples in previous sections have been chosen more for their simplicity than their power. This section shows some examples of more complicated programs capable of performing tasks that would be difficult if not impossible without the availability of these programming features.

Much of the flexibility inherent in programming arises from the ability to embed programming structures inside one another. In Mathcad, you can do this in three ways:

• You can make one of the statements in a program be another program.

• You can define a program elsewhere and call it from within another program as if it were a subroutine.

• You can define a function recursively.

The remainder of this section illustrates these techniques by example.

**Subroutines**

Recall that a program is just an expression made up of statements, each one of which contains an expression. Since a program statement must be an expression, and since a program is itself an expression, it follows that a program statement can be another program.

Figure 18-12 shows two examples of programs containing a statement which is itself a program. The example on the right-hand side of Figure 18-12 shows how to nest programs even more deeply. In principle, there is no limit to how deeply nested a program can be. As a practical matter, however, programs containing deeply nested programs can become too complicated to understand at a glance

One way many programmers avoid overly complicated programs is to bury the complexity in a subroutine. Figure 18-13 shows how you can do something similar in Mathcad. By defining intsimp elsewhere and using it within adapt, the program used to define adapt becomes considerably simpler.

**Recursion**

Recursion is a powerful programming technique that involves defining a function in terms of itself as shown in Figure 18-14, Recursive function definitions should always have at least two parts:

• An initial condition to prevent the recursion from going forever, and

• A definition of the function in terms of a previous value of the function,

The idea is similar to that underlying mathematical induction: if you can get fen + 1) from f(n), and you know f(O) , then you know all there is to know about Keep in mind, however, that recursive function definitions, despite their elegance and conciseness, are not always the most computationally efficient definitions. You may find that an equivalent definition using one of the iterative loops described earlier will evaluate more quickly

**Evaluating programs symbolically**

Like any Mathcad expression, a Mathcad program returns a numerical value when followed by the equal sign. You can al 0 write a Mathcad program to return a symbolic expression when you evaluate it symbolically using the methods described in “Symbolic Calculation.” For example, when you evaluate a program using the live symbolic operator, “~,” Mathcad passes the expression to its symbolic processor and, where possible, returns a simplified symbolic expression. You can use Mathcad’s ability to evaluate programs symbolically to generate complicated symbolic expressions, polynomials, and matrices.

The following simple program substitutes one expression for another and returns a simplified symbolic result:

• Click the button labeled “Add Line” on the Programming Palette

Click outside the expression to see the simplified result

Only one of the variables in this example, x, is defined locally in the program, but Mathcad understands the other variables, a and y, as symbols and substitutes a for x in the expression it returns

Programs that include the return and on error statements cannot be evaluated symbolically since the symbolic processor does not recognize these operators.