Parameter Syntax

To specify a list of function parameters, one can include them in a pair of square brackets and place the brackets right in front of the input arguments list in the function header. The items in the parameter list are separated by commas. The syntax of the simplest form of a parameter item is
parameter_name = intial_value
where parameter_name is a name used to identify the parameter, initial_value is the initial value of the parameter

For example, the following defines a function , with two parameter and .

   f = function [alpha = 3, beta = 5] x -> y
	         y = alpha * x + beta;
       end
A parameterized function behaves like a class member that has attributes that can be accessed using the dot operator. To refer to the value of a parameter alpha of function f outside the function body (in a scope where f is visible), one can use f.alpha. For example;
>> f = function [alpha = 3, beta = 5] x -> y
	         y = alpha * x + beta;  // alpha is the value of the parameter
       end
>> f.alpha
   3
>> f.beta
   5
To refer to the value of parameter alpha inside the function definition body, one can simply state the parameter name, like in the above example. However, if a local variable has the same name, the local variable has higher precedence. In this case, alpha refers to the name of the local variable, while to access the parameter named alpha, one needs to use this.alpha. Of course, this.alpha always refers to a function parameter, whether there is a local variable alpha or not. So the above function can also be written as
   f = function [alpha = 3, beta = 5] x -> y
	         y = this.alpha * x + this.beta;
       end
Inside a function body, the keyword this refers to the function itself. The following example illustrates the difference between a local variable and a parameter.
>>   f = function [alpha = 3, beta = 9] () -> y
            alpha = -5;   /* now alpha is a local variable */
            y = [alpha, this.alpha, beta, this.beta];
     end
>>   f()
       -5    3    9    9

oz 2009-12-22