Procedures

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.

Create a Procedure

If you want to execute a piece of code repeatedly, create and use a procedure. Define a procedure with the proc command. Enclose your code in the begin and end_proc commands:

myProc:= proc(n)
begin
   if n = 1 or n = 0 then
     1
   else
     n * myProc(n - 1)
   end_if;
end_proc:

Use end as a shortcut for end_proc.

Call a Procedure

Now call the procedure:

myProc(5)

Control Return Values

By default, a procedure returns the result of the last executed command. If you want to return other results, use the return command. For example, create a procedure that computes the factorials of integer numbers:

myProcReturn := proc(n)
begin
   if n = 0 or n = 1 then 
      return(1);
   end_if;
   n * myProcReturn(n - 1);
end_proc:

Call the procedure:

myProcReturn(5)

To display the results on your screen without returning them, use the print command:

myProcPrint:= proc(n)
begin
   print(n);
   if n = 0 or n = 1 then 
      return(1);
   end_if;
   n * myProcPrint(n - 1);
end_proc:

Call the procedure:

myProcPrint(5);

Return Multiple Results

To return several results from a procedure, use such structures as lists or matrices as return values:

myProcSort:= proc(a, b)
begin
  if a < b then
    [a, b]
  else
    [b, a]
  end_if;
end_proc:
myProcSort(4/5, 5/7)

Return Symbolic Calls

Many built-in MuPAD® procedures can return symbolic calls to themselves when they cannot compute results as exact values. For example, when you compute sin(PI/2), the sin function returns the exact value 1. At the same time, when you compute sin(x/2), the sin function returns the symbolic call to itself:

sin(x/2)

To enable your custom procedure to return symbolic calls, use the special syntax procname(args()). For example, create the procedure that computes a factorial of its argument:

f := proc(x)
begin
  if testtype(x, Type::PosInt) then
    return(x!)
  else
    return(procname(args()))
  end_if:
end_proc:

If its argument is a positive integer, this procedure returns an exact number:

f(5), f(10)

Otherwise, it does not error, but returns a symbolic call to itself:

f(1/3), f(1.1), f(x), f(x + 1)

Use Global and Local Variables

Inside a procedure, all variables fall into two categories: global and local. Global variables are accessible from everywhere inside a notebook. Local variables are accessible only from within a procedure.

Global Variables

Suppose you want to create a procedure gProc and use the global variable gVar inside the procedure:

gProc := proc(x)
begin
  gVar := gVar^2 + x^2 + 1
end_proc:

When you call this procedure, the value of the variable gVar changes:

gVar := 10;
gProc(5):
gVar

Multiple calls change the value of the global variable further:

gProc(5):
gVar

Note

Avoid using unnecessary global variables.

Global variables reduce code readability, occupy the global namespace, and often lead to errors. When you use global variables inside a procedure, always verify that each call to the procedure changes global variables as intended.

Local Variables

You can access and modify local variables only inside a procedure. Suppose, you use a variable lVar in your notebook:

lVar := 10

To declare a local variable, use the local command inside a procedure:

lProc := proc(x)
  local lVar;
begin
  lVar := 10;
  lVar := lVar^2 + x^2 + 1
end_proc:

When you call this procedure, the value of the variable lVar changes only inside a procedure. Outside the procedure, the variable does not change its value:

lProc(5):
lVar

If you declare a local variable, it does not inherit the value of a global variable with the same name. Local variables are not identifiers of type DOM_IDENT. They belong to a special domain type DOM_VAR. Therefore, you cannot use a local variable as a symbolic variable. Before performing computations with a local variable, you must assign a value to that variable. For example, without the assignment lVar:= 10, the procedure call lProc returns an error message:

lProc := proc(x)
local lVar;
begin
  lVar := lVar^2 + x^2 + 1
end_proc:
lProc(5)
Warning: Uninitialized variable 'lVar' is used.
  Evaluating: lProc
Error: Invalid operand. [_power]
  Evaluating: lProc

Local variables cannot have assumptions.

Restore Values and Properties of Global Variables Modified in Procedures

When you use global variables inside a procedure, you can save their original values and properties, and recover them after the procedure. Suppose, you want to use more decimal digits for calculations with floating-point numbers inside a procedure. By default, the number of digits is 10:

DIGITS

To save this default value, use the save command at the beginning of the procedure:

myProcSave := proc(newDigits, x)
  save DIGITS;
begin
  DIGITS := newDigits;
  print(float(x));
end_proc:

After you call the procedure myProcSave, MuPAD restores the value of the global variable DIGITS:

myProcSave(20, PI);
DIGITS

The combination of save and delete lets you temporarily free the variable for the use inside a procedure. For example, the procedure cannot use the variable x because the variable has a value assigned to it:

x := 10:
proc() 
begin
  solve(x^2 + x = 1, x)
end_proc();
Error: Invalid variable to solve for. [solve]

Use the save command to save the original value 10. Then, free the variable x using the delete command:

x := 10:
proc() 
  save x;
begin
  delete x;
  solve(x^2 + x = 1, x)
end_proc()

After the procedure call, MuPAD restores the original value of x:

x

The save and delete combination is helpful when you want to use a symbolic variable (without any value assigned to it) inside a procedure. You cannot use local variables for that purpose because a local variable in MuPAD is not an identifier. A local variable must have a value assigned to it. Also, you cannot specify assumptions on local variables, and you cannot integrate with respect to local variables.