About Defining a Function (AutoLISP)

You can define your own functions.

Once defined, these functions can be used at the AutoCAD Command prompt, the Visual LISP Console prompt, or within other AutoLISP expressions, just as you use the standard functions.

Note: Visual LISP is available on Windows only.

You can also create your own commands, because commands are just a special type of function. The defun function combines a multiple expressions into a function or command. This function requires at least three arguments:

(defun symbol_name ( arguments / local_variables )
  expressions
)

The following example code defines a simple function that accepts no arguments and displays the message “bye” at the AutoCAD Command prompt. Note that the argument list is defined as an empty list (()):

(defun DONE ( ) (prompt "\nbye! "))
DONE

Once the DONE function is defined, you can use it as you would any other function. For example, the following code prints a message, then says “bye” at the AutoCAD Command prompt:

(prompt "The value is 127.") (DONE) (princ)
The value is 127
bye!

Note how the previous example invokes the princ function without an argument to suppress an ending nil and achieves a quiet exit.

Functions that accept no arguments may seem useless. However, you might use this type of function to query the state of certain system variables or conditions and to return a value that indicates those values.