## `args` -- access procedure parameters

#### Introduction

`args(`0`)` returns the number of parameters of the current procedure.

`args(`i`)` returns the value of the `i`th parameter of the current procedure.

#### Call(s)

`args()`
`args(0)`
`args(i)`
`args(i..j)`

#### Parameters

`i, j` | - | positive integers

#### Returns

`args(`0`)` returns a nonnegative integer. All other calls return an arbitrary MuPAD object or a `sequence` of such objects.

#### Related Functions

`context`, `DOM_PROC`, `DOM_VAR`, `Pref::typeCheck`, `proc`, `procname`, `testargs`

#### Details

- `args` accesses the actual parameters of a procedure and can only be used in procedures. It is mainly intended for procedures with a variable number of arguments, since otherwise parameters can simply be accessed by their names.
- `args(``)` returns an expression sequence of all actual parameters.
- `args(`0`)` returns the number of actual parameters.
- `args(`i`)` returns the value of the `i`th parameter.
- `args(`i..j`)` returns an expression sequence containing the `i`th up to the `j`th parameter.
- In procedures with option `hold`, `args` returns the parameters without further `evaluation`. Use `context` or `eval` to enforce a subsequent evaluation. See example 2.
- `procname`(`args(``)`) returns a symbolic function call of the current procedure with evaluated arguments.
- Assigning values to formal parameters of a procedure changes the result of `args`. Cf. example 4. `args(`0`)` remains unchanged.
- `args` is a function of the system kernel.

#### Example 1

This example demonstrates the various ways of using `args`:

    >> f := proc() begin
         print(Unquoted, "number of arguments" = args(0)):
         print(Unquoted, "sequence of all arguments" = args()):
         if args(0) > 0 then
           print(Unquoted, "first argument" = args(1)):
         end_if:
         if args(0) >= 3 then
           print(Unquoted, "second, third argument" = args(2..3)):
         end_if:
       end_proc:

    >> f():

                              number of arguments = 0

                        sequence of all arguments = null()

    >> f(42):

                              number of arguments = 1

                          sequence of all arguments = 42

                                first argument = 42

    >> f(a, b, c, d):

                              number of arguments = 4

                     sequence of all arguments = (a, b, c, d)

                                first argument = a

                          second, third argument = (b, c)

#### Example 2

`args` does not evaluate the returned parameters in procedures with the option `hold`. Use `context` to achieve this:

    >> f := proc()
         option hold;
       begin
         args(1), context(args(1))
       end_proc:

    >> delete x, y: x := y: y := 2: f(x)

                                       x, 2

#### Example 3

We use `args` to access parameters of a procedure with an arbitrary number of arguments:

    >> f := proc() begin
         args(1) * _plus(args(2..args(0)))
       end_proc:
       f(2, 3), f(2, 3, 4)

                                       6, 14

#### Example 4

Assigning values to formal parameters affects the behavior of `args`. In the following example, `args` returns the value `4`, which is assigned inside the procedure, and not the value `1`, which is the argument of the procedure call:

    >> f := proc(a) begin a := 4; args() end_proc:
       f(1)

                                         4

#### Changes

- Assignments to formal parameters now affect `args`.

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