(defun [c:] name ([arg1 arg2 ...] / [local-var1 local-var2 ...]) expression)
Create a LISP function.
When you write a LISP file, the file itself is not actually the function. Rather, one LISP file contains one or more functions (or programs) that are all available when the file is loaded with the load function.
The name of a function must be defined in the first statement using the defun function. This function is the first actual command within the program. It begins with an open parenthesis, which closes at the end of the program.
The defun function is followed by the name of your function or program. Once a name is selected, you have a few choices:
- First, you can do nothing by adding ( ). This indicates that any variables used in the program are global, meaning they don't lose their value once the program ends. For example, the variable A defined as 12 would be 12 for any other program in the LISP file as well:
(defun newfunc ( )
...
)
- Another option is to make all variables local to the program. For instance, if you defined N to equal 15, it would be 15 only for this particular program while it is running. To show that the variable is local, precede it with a forward slash (/) after the name of the program:
(defun newfunc ( / N)
...
)
- Sometimes you may want to pass an argument to the program. This means that the variable will be assigned a value from outside of the currently running program. Place these variables before the slash:
(defun newfunc (A / N)
...
)
Once your function is defined with all its variables, you can choose to use the C: option. Prefixing the C: to the function name enables the command name to be entered without parenthesis at the command prompt of progeCAD. This makes it look like any other progeCAD command such as Line, Arc, and Circle.
(defun C:newfunc ( A / N)
...
)
NOTE The C: has nothing to do with your computer's C: drive. In LISP, it is short for "command" and tells progeCAD to treat the LISP function like an progeCAD command.
Following the lists of variables, the expression that will make up the body of the program must be entered before the closing parenthesis.
Examples
Code | Results: |
---|---|
(defun newfunc ( ) . . .) | May use global variables. |
(defun newfunc (var1 var2). . . ) | Newfunc receives two outside values. |
(defun newfunc (/ var1 var2). . .) | Newfunc has two local variables. |