This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Protect Function and Option Names

The names of the built-in MuPAD® functions and options are protected. If you try to assign a value to a MuPAD function or option, the system throws an error. This approach ensures that you will not overwrite a built-in function or option accidentally.

If you create a new MuPAD procedure, it is recommended to protect the procedure and all its options, especially if you often use that procedure. For example, MuPAD does not provide a function for computing Lucas numbers. You can write your own procedure for computing Lucas numbers, and then protect the procedure name.

The Lucas numbers are a sequence of integers. The recursion formula that defines the nth Lucas number is similar to the definition of the Fibonacci numbers:

Create the following procedure that computes the nth Lucas number:

lucas:= proc(n:Type::PosInt)
  option remember;
  if n = 1 then
  elif n = 2 then
    lucas(n - 1) + lucas(n - 2)
lucas(i) $ i = 1..5

Now protect the procedure name, lucas, using protect with the ProtectLevelError option:

protect(lucas, ProtectLevelError):

ProtectLevelError lets you set full protection for the identifier. Now, trying to assign any value to lucas results in error:

lucas := 0
Error: The identifier 'lucas' is protected. [_assign]

Alternatively, you can use the ProtectLevelWarning option. In this case, you can still assign a value to the protected identifier, but a warning appears, for example:

protect(lucas, ProtectLevelWarning):

You can assign any value to lucas now, but such assignment triggers a warning:

lucas := 0
Warning: The protected variable 'lucas' is overwritten. [_assign]

For further computations, remove protection from the identifier lucas:

Was this topic helpful?