| 15 | |
| 16 | The pan language permits user-defined functions. These functions are essentially a DML block bound to an identifier. Only one DML block may be assigned to a given identifier. Attempts to redefine an existing function will cause the execution to be aborted. The syntax for defining a function is: |
| 17 | {{{ |
| 18 | function identifier = DML; |
| 19 | }}} |
| 20 | where identifier is a valid pan identifier and DML is the block to bind to this identifier. |
| 21 | |
| 22 | When the function is called, the DML will have the variables 'ARGC' and 'ARGV' defined. (The deprecated names 'argc' and 'argv' will also be defined.) The variable 'ARGC' contains the number of arguments passed to the function; 'ARGV' is a list containing the values of the arguments. |
| 23 | |
| 24 | The pan language does no automatic checking of the number or types of arguments. The DML block that defines the function must make all of these checks explicitly and use the error() function to emit an informative message in case of an error. |
| 25 | |
| 26 | Recursive calls to a function are permitted. However, the call depth is limited (by an option when the compiler is invoked) to avoid infinite recursion. Typically, the maximum is a small number like 10. |
| 27 | |
| 28 | The following example defines a function that checks if the number of arguments is even and are all numbers: |
| 29 | {{{ |
| 30 | function even_numbers = { |
| 31 | |
| 32 | if (ARGC%2 != 0) { |
| 33 | error('number of arguments must be even'); |
| 34 | }; |
| 35 | |
| 36 | foreach (k, v, ARGV) { |
| 37 | if (! is_number(v)) { |
| 38 | error('non-numeric argument found'); |
| 39 | }; |
| 40 | }; |
| 41 | }}} |
| 42 | |