Access Arguments of a Procedure

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.

The terms parameters and arguments are often used interchangeably. Strictly speaking, the term parameters means object names that you specify while defining a procedure. The term arguments means objects that you use while calling a procedure. This documentation also uses the term formal parameters for parameters and the term actual parameters for arguments.

Many programming languages require the number of parameters to be equal to the number of arguments. MuPAD® does not have this requirement. Therefore, you can call a procedure with the number of arguments different from the number of specified parameters. If you call a procedure using fewer arguments than parameters, the system treats additional parameters as additional local variables without specified values. If you call a procedure using more arguments than parameters, the system does not disregard these additional parameters, but enables you to access them.

The args function lets you access the arguments of a current procedure call. The call args(i), where i is a positive integer, returns the ith argument of the current procedure call. The call args(0) returns the total number of arguments in the current procedure call. For example, the following procedure computes the sum of its arguments in each procedure call:

f := proc() begin
  _plus(args(i) $ i = 1..args(0)):

The procedure works for any number of arguments:

f(), f(100), f(1, 2, 3, 4, 5),
f(a, b, c), f(x $ x = 1..1000)

Also, you can access the whole sequence of arguments or any subsequence of that sequence. For example, the following procedure prints all arguments used in the current call. If the current call uses three or more arguments, the procedure also prints its first three arguments:

g := proc() begin
print(Unquoted, "all arguments" = args()):
  if args(0) > 2 then
    print(Unquoted, "first three arguments" = args(1..3)):
    print(Unquoted, "not enough arguments"):

Call the procedure g with five arguments:

g(10, 20, 30, 40, 50)
all arguments = (10, 20, 30, 40, 50)
first three arguments = (10, 20, 30)

When you pass arguments to a procedure, MuPAD evaluates these arguments. Then the system creates a local variable for each formal parameter specified in the procedure definition. The system assigns evaluated arguments to these local variables. Parameters outside the procedure do not change. For example, assign a new value to a formal parameter inside the procedure:

h := proc(a) begin
  a := b;

Assigning a new value to a formal parameter inside a procedure does not affect the parameter itself. This assignment affects the result returned by args. For example, if you pass any argument to the procedure h, that argument changes to a variable b inside the procedure:


The formal parameter a does not change its value outside the procedure:


For further computations, delete the procedures f, g, and h:

delete f, g, h