Another rule may be called within any do block. Functions have the following generalized syntax.

FunctionName (arguments)
do
{
 ... ASL statement(s) ...
}

Any valid ASL expression may be used in the body of a function. This includes calls to other functions and recursive function calls.

Function may have any number of arguments, including no arguments, which are any data type that is supported by ASL.

Functions optionally return a value, which are also of any ASL data type. The return value may be a constant, or a variable declared within the scope of the function do block, as described in “Returning values” on page 92.

A function must be defined in the same ASL script where it is called, and may be defined before or after the location where it is referenced. The general syntax used to invoke a function references the function by name, passes the number and type of arguments expected by the function, and handles the return value, as shown.

Note:

Not all functions are required to return a value as shown in the example.

do {
  ... ASL statement(s) ...
  arg1 = x;
  arg2 = y;
  ReturnValue = FunctionName (arg1, arg2);
  ... handle ReturnValue ...
}

A function may be defined only outside of a do block. That is, a function may not be defined within another function with the intent of referencing the inner function from another do block.

The following is an example of a recursive function definition and call.

START
{  .. eol  }
do {
  foreach i (list(0,1,2,3,4,5)) {
   print (i.”! = “.factorial(i));
  }
}
factorial(x)
do {
  if (x > 1)
   return (x * factorial(x-1));
  else
   return (x);

}

This example produces the following results.

0! = 0
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120