Function Definitions

 13  -  Function Related

defun ..... Defines a function or an AutoLISP command.  The first argument is the name of the new function being defined. Avoid using names already used by built-in functions.  The second argument is a list of the variables that will be assigned values to be used in the function when it is called.  (This can be a null list.)  The remaining expression(s) are executed when the function is called.

(defun double (n) (* n 2.0))

The above expression defines a function named double.  It accepts a number (which is assigned to variable n) and returns the number multiplied by 2.  This function would be called  just like any built-in function.  The function name is placed first in a list, then the number being sent to it.  For example,  (double 18)  would return 36.  The 18 is temporarily assigned to variable n.

(defun hypot (a b) (sqrt (+ (* a a) (* b b))))

The above expression defines a function named hypot (short for hypotenuse).  It accepts two numbers (a and b, which represent the length of the sides of a right triangle) and returns the length of the hypotenuse, that is, the square root of the sum of a2 and b2.  This function would be called just like any built-in function.  The function name is placed first in a list, then the two numbers that are being sent to it.  For example,  (hypot 5 12) would return 13.0.  The numbers 5 and 12 are temporarily assigned to variables a and b respectively.

Values sent to a function are assigned to the variables according to the order in which the variables are listed in the variable list (immediately following the name of the function) and according to the order in which they are listed when the function is called.  Thus, in the last example, since variable a is listed first in the function definition, and 5 is listed first when the function is called, 5 is assigned to a.  (In this particular example the result is the same whether the 5 is listed first or the 12, but in many functions, the order is important.)

The values that are sent to the function are assigned to variables a and b as local variables. If variable a or b is already assigned a value outside this function, that value is temporarily set aside, the new value is assigned for the duration of the function, then the former value is recaptured for the variable.

A function can be defined as an AutoLISP command by placing "c:" in front of the function name, as follows:

(defun c:hypot
   (setq a (getreal "\nEnter length of side a: "))
   (setq b (getreal "\nEnter length of side b: "))
   (princ "\nHypotenuse = ")
   (sqrt (+ (* a a) (* b b)))

Since the above function is defined as an AutoLISP command, it can be called simply by entering HYPOT at the command prompt (no parentheses needed). Notice that the list of variables is empty -- the values of a and b must be obtained within the command.

Avoid using the names of already-existing commands when you define new AutoLISP commands.

Variables that are assigned values with the setq function are global variables. That is, their value assignments hold outside the function. To change them to local variables, list them after a slash in the variable list, as follows: (defun c:hypot (/ a b) ...    Local variables save heap space and will not affect other variables with the same names outside the function.

load ..... Loads AutoLISP program files into the drawing editor  (An AutoLISP program file usually has the extension ".LSP" which is assumed as the default by the load function.  However, files with other extensions can be loaded as long as the extension is included in the load function.)  One or several programs can be defined in a file.  When a file that contains several programs is loaded, only the name of the last function defined is displayed.

(load "strap")  loads the file STRAP.LSP and returns the name of the last function defined in the file
(load "a:sweep1")  loads the file SWEEP1.LSP from drive a:
(load "\\pro\\sweep2")  loads the file SWEEP2.LSP from folder \PRO
(load "/pro/sweep2")  same as above
(load "strap.alp")  loads the file STRAP.ALP

The load function searches all folders on the AutoCAD search path.

An additional second argument can be used which is evaluated by AutoLISP if the load operation fails. This argument can be a string to be printed on the screen or a function to be evaluated. For example, if the file MAGIC.LSP does not exist,

(load "magic")  produces the usual error message
(load "magic" "OOPS")  returns "OOPS"
(load "magic" (repeat 3 (princ "failure ")))  prints failure failure failure , and returns "failure "

If you want your AutoLISP functions and commands to load automatically, place them in a file called ACAD.LSP on the AutoCAD search path.

autoload ..... Sets up automatic loading of a file when a command in that file is entered

(autoload "utility" (list "aaa" "bbb"))  sets up the automatic loading of file UTILITY.LSP when either AAA or BBB is first entered at the Command prompt.  In the UTILITY.LSP file, aaa and bbb must be defined as commands using "c:".  See defun above.

progn ..... Groups one or more expressions into a single expression and returns the value of the last expression

progn is useful in situations where several expressions are needed, but the format of a function allows for only one, such as

(if (or (< x 5.0) (> x 10.0))
      (prompt "Value is out of range.")
      (setq c1 (1+  c1))
      (prompt "Value is within range.")
      (setq c2 (1+  c2))

exit ..... Exits the current function, returns to the command prompt

(exit)  aborts the current AutoLISP program, triggers the message "quit/exit abort", dumps program code to the screen, and returns to the AutoCAD command prompt

quit ..... Same as exit.  See exit above.

apply ..... Applies the quoted function to the elements in the quoted list.  Works with user-defined functions as well as built-in functions.

(apply '*  '(2 4 6))  returns 48
(apply '-  '(2 4 6))  returns -8
(apply 'and '(3.75 T "door"))  returns T

mapcar ..... Executes a function by supplying the first element extracted from each list, then the second, etc. and returns a list of the results

The number of lists must be appropriate to the number of arguments that can or must be handled by the function. If the lists are not the same length, the function will be applied until the shortest list runs out of elements.

(mapcar '1-  '(2 4 6))  returns (1 3 5)
(mapcar '*  '(12 10 8) '(1 2 3))  returns (12 20 24)
(mapcar 'max  '(90 75 95) '(88 89 90) '(-100 0 100))  returns (90 89 100)

lambda ..... Defines a temporary function

lambda is typically nested in an apply or mapcar function as illustrated below. The format of lambda is identical to the format of defun, except there is no name, since lambda defines a temporary function that is not stored separately.

(apply '(lambda (p q r)
               (setq m (sqrt (+ p q)))
               (setq n (+ p q (* r 2.0)))
    '(2 4 6))
The above expression returns 18 (only the result of the last expression in lambda is returned).

(mapcar '(lambda (x y)
               (* x y))
       (1 2 3 4) '(5 4 3 2))
The above expression returns (5 8 9 8).


Copyright © 1988, 1998 Ronald W. Leigh