## `testargs` -- decide whether procedure arguments should be tested

#### Introduction

Inside a procedure, `testargs` indicates whether the procedure was called on the interactive level.

#### Call(s)

`testargs()`
`testargs(b)`

#### Parameters

`b` | - | `TRUE` or `FALSE`

#### Returns

`TRUE` or `FALSE`.

#### Related Functions

`proc`, `testtype`, `Pref::typeCheck`

#### Details

- Checking the input parameters of a procedure may be costly. For this reason, most functions of the MuPAD libraries are implemented according to the following philosophy:

  If a procedure is called on the interactive level, i.e., if its parameters are supplied interactively by the user, then the parameters should be checked. If the input parameters do not comply with the documented specification of the procedure, then appropriate error messages should be returned to notify the user of wrong usage.

  If the procedure is called by another procedure, then no check of the parameters should be performed to improve efficiency. The calling procedure is supposed to make sure that appropriate parameters are passed.

  `testargs` is the tool to check whether the arguments should be tested: called inside the body of a procedure, `testargs(``)` returns `TRUE` if the procedure was called on the interactive level. Otherwise, it returns `FALSE`.

- `testargs` has two modes. In the ``standard mode'', its functionality is as described above. In the ``debugging mode'', the call `testargs(``)` always returns `TRUE`. This supports the debugging of procedures: any function using `testargs` checks its parameters and returns useful error messages if called in an inappropriate way.

  The call `testargs(`TRUE`)` switches to the ``debugging mode'', i.e., parameter testing is switched on globally.

  The call `testargs(`FALSE`)` switches to the ``standard mode'', i.e., parameter testing is used only on the interactive level.

  The call `testargs(`b`)` returns the previously set value.

- Checking the input parameters of a procedure can also be controlled with the function `Pref::typeCheck`.
- `testargs` is a function of the system kernel.

#### Example 1

The following example demonstrates how `testargs` should be used inside a procedure. The function `p` is to generate a sequence of `n` zeroes; its argument should be a positive integer:

    >> p := proc(n)
       begin
          if testargs() then
              if not testtype(n, Type::PosInt) then
                 error("expecting a positive integer");
              end_if;
          end_if;
          return(0 $ n)
       end_proc:

Its argument is checked when `p` is called on the interactive level:

    >> p(13/2)

          Error: expecting a positive integer [p]

Calling `p` from within a procedure with an inappropriate parameter does not invoke the argument testing. The following error message is not issued by `p`. It is caused by the attempt to evaluate `0 $ n`:

    >> f := proc(n) begin p(n) end_proc: f(13/2)

          Error: Illegal argument [_seqgen];
          during evaluation of 'p'

We switch on the ``debugging mode'' of `testargs`:

    >> testargs(TRUE):

Now also a non-interactive call to `p` produces an informative error message:

    >> f(13/2)

          Error: expecting a positive integer [p]

We clean up, restoring the ``standard mode'' of `testargs`:

    >> testargs(FALSE): delete f, g:

#### Changes

- no changes

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