testargs

Decide whether procedure arguments should be tested

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.

Syntax

testargs()
testargs(b)

Description

Inside a procedure, testargs indicates whether the procedure should check its arguments.

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 “argument checking 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 “argument checking 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.

testargs should not be used to change the behavior of a function other than performing type-checks, since the user may have switched to “argument checking mode”.

Checking the input parameters of a procedure can also be controlled with the function Pref::typeCheck.

Examples

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 strange output is caused by the attempt to evaluate 0 $ n:

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

We switch on the “argument checking 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:

Parameters

b

TRUE or FALSE

Return Values

TRUE or FALSE.

See Also

MuPAD Functions