Contents Previous Next Subchapters

Defining Functions With Variable Numbers Of Arguments
Syntax arg(value)
See Also arguments , nargin , nargout

Description
References the specified function argument, where value is an non-negative integer scalar.

Number Of Arguments
If a function call does not have multiple return values , the expression arg(0) is equal to the number of arguments in a function reference. If you enter
     clear
     function f(a, b, c, d, e) begin
          print arg(0)
     end
     f(2, 4, 6)
O-Matrix will respond
     3

Referencing Arguments By Index
The expression arg(1) is equal to the first argument of the function, arg(2) is equal to the second argument, and so on.

If you enter
     clear
     function maximum() begin
          imax = 1
          for i = 2 to arg(0) begin
               if arg(i) > arg(imax) then imax = i
          end
          return arg(imax)
     end
     maximum(1., 5, 2., 6)
O-Matrix will respond
     6

Reference
If an argument is a simple variable name, you can assign a new value to the argument using this notation (see arguments, Reference for a discussion of a simple variable). For example, if you enter
     clear
     function f() begin
          arg(2) = 3
     end
     x = 5
     y = 6
     f(x, y)
     print x, y
O-Matrix will respond
     5 3