Both sides previous revisionPrevious revisionNext revision | Previous revision |
ch2_1_defun [2022/03/31 04:55] – admin | ch2_1_defun [2022/03/31 04:57] (current) – admin |
---|
**''DEFUN''** | **''DEFUN''** |
====== '' DEFUN '' ====== | |
| |
''DEFUN'' form defines a named ordinary function object in global scope. It needs at least one symbol argument, which is not evaluated and will be the name of the function. Defun with no other arguments creates a function that evaluates to ''NIL''. For any practical use, we need to supply more arguments. Second argument must then be a list of parameters (symbols) of the function, the symbols are not evaluated and will be used for local binding of parameters once the defined function is called. In fact, no parts of the ''DEFUN'' expression are evaluated during the definition. Third argument is optional description string. If the third argument is not string, it is considered first form of the function body. Any other parameters are to ''DEFUN'' are stored as the function body. | ''DEFUN'' form defines a named ordinary function object in global scope. It needs at least one symbol argument, which is not evaluated and will be the name of the function. Defun with no other arguments creates a function that evaluates to ''NIL''. For any practical use, we need to supply more arguments. Second argument must then be a list of parameters (symbols) of the function, the symbols are not evaluated and will be used for local binding of parameters once the defined function is called. In fact, no parts of the ''DEFUN'' expression are evaluated during the definition. Third argument is optional description string. If the third argument is not string, it is considered first form of the function body. Any other parameters are to ''DEFUN'' are stored as the function body. |
If function body contains more expressions, all are evaluated sequentially (once the function is called), but only the output of the last one is returned from the function (this behavior is due to implicit ''PROGN''). | If function body contains more expressions, all are evaluated sequentially (once the function is called), but only the output of the last one is returned from the function (this behavior is due to implicit ''PROGN''). |
| |
====== Optional parameters ====== | === Optional parameters === |
| |
Since LabLISP version 1.3, it is possible for user to define function with variable number of parameters, using a symbols ''&OPTIONAL'' and ''&REST'' in the list of parameters. | Since LabLISP version 1.3, it is possible for user to define function with variable number of parameters, using a symbols ''&OPTIONAL'' and ''&REST'' in the list of parameters. |
| |
The mandatory, optional and rest paramters can be combined. Order of the keywords ''&OPTIONAL'' and ''&REST'' must be kept. That means we can have any number of mandatory parameters, then ''&OPTIONAL'' keyword, after any number of optional parameters, with or without initializers, then ''&REST'', which must be followed by exactly one symbol. | The mandatory, optional and rest paramters can be combined. Order of the keywords ''&OPTIONAL'' and ''&REST'' must be kept. That means we can have any number of mandatory parameters, then ''&OPTIONAL'' keyword, after any number of optional parameters, with or without initializers, then ''&REST'', which must be followed by exactly one symbol. |
| |
| === Lexical closure === |
| |
Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, where the function was defined, and not in the environment where called. | Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, where the function was defined, and not in the environment where called. |