`MAXDEPTH`

Prevent infinite recursion during procedure calls

**MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.**

**MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.**

The environment variable `MAXDEPTH`

determines
the maximal recursion depth of nested procedure calls. When this recursion
depth is reached, an error occurs.

Possible values: Positive integer; the maximum value depends on the operating system, see below.

The purpose of `MAXDEPTH`

is to provide a heuristic
for recognizing infinite recursion with respect to procedure calls, like in ```
p := x
-> p(x): p(0)
```

. If, in this example, the recursion depth
would not be limited, then the procedure `p`

would
call itself recursively infinitely often, and the system would “hang”.

If during the evaluation of an object the recursion depth `MAXDEPTH`

is
reached, then the computation is aborted with an error.

Similarly, the environment variable `MAXLEVEL`

provides a
heuristic for recognizing infinite recursion with respect to the substitution
of values for identifiers; see the
corresponding help page for details and examples.

The default value of `MAXDEPTH`

is `500`

; `MAXDEPTH`

has
this value after starting or resetting the system via `reset`

. Also the command ```
delete
MAXDEPTH
```

restores the default value.

`MAXDEPTH`

is a global variable. Use the statement `save MAXDEPTH`

in
a procedure to confine any changes to `MAXDEPTH`

to
this procedure.

The maximum value of `MAXDEPTH`

depends on
the operating system. Under Windows^{®} it is 2^{11} =
2048. Under UNIX^{®} operating systems the maximum
value depends on the maximum size of the C-stack. With a default stack
size of `8 MB`

the value is 2048,
too; with a bigger stack size it can be bigger (in a bash the stack
size can be set with `ulimit -s`

).

Evaluation of objects defined by an infinite recursion produces an error:

p := proc() begin p() end_proc: p()

Error: Recursive definition: Reached maximal depth for nested procedure calls. Evaluating: p

This also works for mutually recursive definitions:

p := proc(x) begin q(x + 1)^2 end_proc: q := proc(y) begin p(x) + 2 end_proc: p(0)

Error: Recursive definition: Reached maximal depth for nested procedure calls. Evaluating: p

If the maximal recursion depth is reached, then this does not necessarily mean that infinite recursion is involved. The following recursive procedure computes the factorial of a nonnegative integer. If we set the maximal recursion depth to a smaller value than necessary to compute , then an error occurs:

factorial := proc(n) begin if n = 0 then 1 else n*factorial(n - 1) end_if end_proc: MAXDEPTH := 4: factorial(5)

Error: Recursive definition: Reached maximal depth for nested procedure calls. Evaluating: factorial

If we set `MAXDEPTH`

to 5,
then the recursion depth is big enough for computing .
The command `delete MAXDEPTH`

resets `MAXDEPTH`

to
its default value `500`

:

MAXDEPTH := 5: factorial(5); delete MAXDEPTH: