## `Pref::warnDeadProcEnv` -- warnings about wrong usage of lexical scope

#### Introduction

`Pref::warnDeadProcEnv(``)` returns the current setting.

`Pref::warnDeadProcEnv(`TRUE`)` switches on warnings about unreachable procedure environments.

`Pref::warnDeadProcEnv(`FALSE`)` switches warning messages off.

`Pref::warnDeadProcEnv(`NIL`)` will reset the default value, which is `FALSE`.

#### Call(s)

`Pref::warnDeadProcEnv()`
`Pref::warnDeadProcEnv(TRUE)`
`Pref::warnDeadProcEnv(FALSE)`
`Pref::warnDeadProcEnv(NIL)`

#### Returns

the previously defined value; `TRUE` or `FALSE`

#### Side Effects

Allows or suppresses warning messages.

#### Further Documentation

changes

#### Related Functions

`Pref::warnChanges`, `Pref::warnLexProcEnv`, `proc`

#### Details

- If a procedure is executed a procedure environment is created for this procedure. It contains the current values of formal parameters and local variables. On exit of the procedure this procedure environment is normally not needed any more and destroyed.
- If a procedure returns a local procedure as its result, this local procedure escapes its scope. Usually this is no problem. Only if the escaping procedure contains references to formal parameters or local variables of the outer procedure these variables escape their scope. These variables can not be dereferenced since they reference values of a procedure environment of the outer procedure which does not exist any more.
- Use option escape in the outer procedure in order to keep its procedure environment untouched.

#### Example 1

Here we write procedure `p` which returns a local procedure. The returned procedure adds the value of its argument `y` to the value of the argument `x` of the first procedure. The following naive implementation produces a strange output and, when the resulting procedure is called, a warning message and an error:

    >> Pref::warnDeadProcEnv(FALSE):
       p := proc(x) begin y -> x + y end:
       f := p(1);  f(2)

                               y -> DOM_VAR(1,2) + y
          Warning: Uninitialized variable 'unknown' used;
          during evaluation of 'f'
          Error: Illegal operand [_plus];
          during evaluation of 'f'

If `Pref::warnDeadProcEnv` is set to `TRUE` MuPAD will print a warning message when the local procedure escapes its scope:

    >> Pref::warnDeadProcEnv(TRUE):
       p := proc(x) begin y -> x + y end:
       f := p(1)

          Warning: Found dead closure of procedure 'p'

                               y -> DOM_VAR(1,2) + y

Use option escape in the outer procedure to prevent this warning. The returned procedure `f` will then work as expected:

    >> p := proc(x) option escape; begin y -> x + y end:
       f := p(1);  f(2)

                                    y -> x + y

                                         3

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000