Previous Page Next Page Contents

args -- access procedure parameters

Introduction

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

args(i) returns the value of the ith 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

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




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000