Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 01/15/2022 in all areas

  1. Did you try to evaluate the code? If so, you would have answered your own question - the function must be defined before it is evaluated, else the interpreter will report a 'no function definition' error. That's not to say that it has to be the first expression within the outermost defun expression, but only that it must be defined before the function is evaluated, e.g.: (defun main ( / a ) (setq a 2.0) (defun foo ( x ) (+ x 2) ) (foo a) ) Unless the symbols used to represent the functions are declared local to the outer defun expression, they will be defined within the document namespace (with the same scope as the outer defun, or as a global variable); declaring such symbols as local to the function in which they are defined will restrict the scope of their definition to that function, with the symbols reverting to their previous value outside of the scope of the function (which will likely be nil, but not necessarily). Observe the following: (defun foo ( x ) (+ x 3) ) (defun main ( / a foo ) (setq a 2.0) (defun foo ( x ) (+ x 2) ) (foo a) ) _$ (foo 2.0) 5.0 _$ (main) 4.0 This highlights a potential issue where function naming conventions are concerned and motivation for declaring functions as local to the function in which they are evaluated, since commonly named functions may otherwise overwrite each other's definitions within the document namespace (in the same way that global variables may be overwritten). The drawback to declaring a function as local is efficiency, as the function is redefined with every evaluation of the function in which it is locally declared.
    1 point
×
×
  • Create New...