HyperMath

Variable Number of Arguments

Variable Number of Arguments

Previous topic Next topic No expanding text in this topic  

Variable Number of Arguments

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

It is often useful to have variable numbers of arguments to a function.  This is done by using for the arguments such as:

  function function_name(...)

  body

  end

Inside the function, the keyword arg (a table itself) can be used to access the arguments.  HyperMath inserts each argument passed to the function in arg in the order they appear.  So the first element can be accessed by indexing into arg as in arg[1] and so on.  For example:

  > function foo(...) print(arg[1]) end

  > foo(3,77)

  3

The number of arguments passed  can be obtained from arg.n, a special entry added to arg automatically.

  > function foo(...) print("No. of arguments:", arg.n) end

  > foo(3,77)

  No. of arguments: 2