Note on Function Syntax

In Lua a function can have more than one signature. This provides considerable flexibility in function notation. Changing the number of arguments changes how a function works. Moreover, the data types for an argument can change as well. Furthermore, Lua provides support for named arguments through use of tables.

Named arguments

The following command uses named arguments (note the opening and closing brackets).

>>std.pnorm{q=, mean=0, sd=1} → number

The function pnorm is under std library. By using the “{“ and “}” it allows name arguments. The names (as a matter of fact, these are Lua table keys) are: q, mean and sd. The names mean and sd have default values of 0 and 1 respectively, therefore, if not supplied, these values will be used. However, the name q must be provided. Also note that, when using name arguments, the order of the names does not matter. Finally the right arrow indicates that the function returns a number.

 

Notice the variation in using the std.pnorm command with the name arguments:

>>std.pnorm{q=0.5, mean=0, sd=1}
0.691462

>>std.pnorm{q=0.5, sd=1, mean=0} --changing the argument order
0.691462

>>std.pnorm{q=0.5, mean=0} -- not providing sd
0.691462

>>std.pnorm{q=0.5} -- not providing sd and mean
0.691462

>>std.pnorm{mean=0, sd=1} -- not providing q
ERROR: First argument (q) must be either a number or of type Vector

Please note that in the last call, the mandatory name q was not provided, therefore, the function threw and error.

 

Regular function call

Here, the term regular function call is used since in Lua every function call is surrounded with parenthesis, (and ). The so-called named argument call std.pnorm{} is just a syntactic sugar mechanism provided by Lua for the function call std.pnorm({}).

Notice the use of parenthesis in the following command:

>>std.pnorm(q=, mean=0, sd=1) → number

The function pnorm is under std library. Since "(" and ")" brackets are used, named arguments cannot be used. The following cases are possible:

  1. If 1 argument is provided: The argument will be considered as q and mean and sd will be considered as 0 and 1 respectively.
  2. If 2 arguments are provided: First argument will be taken as q and second argument will be mean and sd will be considered as 1.
  3. If 3 arguments are provided: First one is q, second is mean and third one is sd.

>>std.pnorm(0.5) --only q is provided. By default mean=0 and sd=1
0.691462

>>std.pnorm(0.5,0) -- q=0.5 and mean=1 are provided. By default sd=1
0.691462

>>std.pnorm(0.5,0,1) -- q=0.5, mean=0 and sd=1 are provided
0.691462

>>std.pnorm(0.5,1,0) -- Changing the order. q=0.5, mean=1 and sd=0
ERROR: Standard deviation must be greater than zero.

Please note that, changing the order of the arguments changes the value of the arguments. Therefore, for accuracy, all arguments must be provided in the mentioned order.