HyperMath

Passing Arguments

Passing Arguments

Previous topic Next topic No expanding text in this topic  

Passing Arguments

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

Arguments are passed in the order defined in the function declaration, separated by commas.  HyperMath deals with mismatched numbers by calling and formal function arguments.  Formal function arguments receive a default value of nil if they are not filled.  When too many arguments are passed, they are simply ignored.  No type is specified for each argument as they are dynamically typed. You only need to know the type of an object when it is used, not when it is referenced.  The following function is an example:

> function foo(a,b,c) print(a,b,c) end

Here's what happens when we call it with no arguments:

> foo()

nil     nil     nil

Notice that each of the arguments default to the value nil, or no value, and there is no error generated.  Here is an example of what happens when too many arguments are passed:

> foo(1,2,3,4)

1       2       3

No error is generated and the last argument is ignored.  Because HyperMath is dynamically typed, you can pass any argument type, such strings and numbers.

> foo("hello")

hello   nil     nil

> foo("pi", 3.1415, {"this is a table" })

pi      3.1415  table: 002FDBE8

Note

For arguments of type table, a reference is passed instead of a copy of the data.  Hence, the linkage with the original table is retained; changing the individual elements of passed arguments inside the function will change the original data. To pass a copy of the table, use CopyTable() function.

As a good practice, it is not recommended that the argument pass by reference mechanism be used to update variables; instead the argument should be copied inside the function, and then updated and returned.  The output should be assigned to the original variable during the function call.