AfraLISP - Learn AutoLISP for AutoCAD productivity

The Define Function (defun)

by Kenny Ramage

We've all seen something like this :

(defun C:DDSTEEL ( / t1 t2 h1 h2 ang)

This, of course, is the first line of an AutoLISP routine.
But, do you know what it all means?
When I started writing AutoLISP code, I was totally confused!!
The more I read, the more in a muddle I got.
Global, Local, Argument, Define, Aghhh…
So, if you are as confused as I was, read on.

Let's start at the very beginning. (A very good place to start, say you!)
The name of a program, or function, must be defined in the first statement, which is done by using the command :

(defun [Define Function]

Why is there not a closing parenthesis after the (defun command?
There is! In fact the last parenthesis in the program is the one that closes (defun.

Anyway, let's carry on.
After (defun must come the name of the program :

(defun DDSTEEL

You do realise that the name of the AutoLISP file, is not necessarily the name of the program. In fact, one AutoLISP file can have several programs inside.

Hey, that was easy, I hear you say. Now comes the hard part!!
After you've named your program you have three choices.

First, do nothing by using ( ) :

(defun DDSTEEL ( )

What you are saying here is that every variable that you use in your function is GLOBAL. A GLOBAL variable is one that doesn't lose it's value when the program ends. For example, if PT3 was defined as 45.2 in your program, it would still have the value of 45.2 when your program ends and would retain that value until you replace it with another value or, start a new drawing.

Secondly, you can declare your variables as LOCAL.
To do this you precede your variables with / :

(defun DDSTEEL ( / p1 p2 p3)

The following example shows the use of LOCAL symbols :

	(defun TEST ( / ANG1 ANG2)
	     (setq ANG1 "Monday")
	     (setq ANG2 "Tuesday")
	     (princ (strcat "\nANG1 has the value " ANG1))
	     (princ (strcat "\nANG2 has the value " ANG2))

Now, at the AutoCAD command line, assign the variables ANG1 and ANG2 to values other than those used by the TEST function :

Command: (setq ANG1 45)
Command: (setq ANG2 90)

Verify their values :

Command: !ANG1 [Should return 45.0]
Command: !ANG2 [Should return 90.0]

Now run our function :

Command: (load "TEST")
Command: (TEST)

The function should return :

ANG1 has the value Monday
ANG2 has the value Tuesday

Now check the current values of ANG1 and ANG2 :

Command: !ANG1
Command: !ANG2

You will find that they still have the values of 45.0. and 90.0 respectively.

A LOCAL variable is one that has a value only for that program while the program is running.

The third option is to list a variable without the /.
This means that a variable is set up to receive a value passed to it from outside the program. eg :

	(defun DTR (a)

          (* PI (/a 180.0))

To use this, we must pass the value of the argument to the function :

(DTR 90.0)

Here is another example :

Let us write a function that calculates the area of a circle.
The formulae for calculating the area of a circle is:

Area = PI x radius squared or, PI x r x r

Our program would look like this :

	(defun acirc (r)
           (setq a (* (* r r) PI))
	   (setq a (rtos a))
           (princ (strcat "\nArea = " a))

Now try it :

Command: (load "acirc")
Command: (acirc 24)

It should return :

Area = 1809.6

You can, of course combine all three options, for example :

(defun DDSTEEL ( a / b c d)

DDSTEEL is the name of the function. Variable a is an argument and receives the first value passed to it from outside the program.
Variables b, c, and d are all locals and lose their values once the program has ended.

Another option that can be used with (defun is if the name of the function is preceded with C:

(defun C:DDSTEEL ()

Because of the C: you don't have to use parenthesis to call the function.
AutoCAD now thinks of that function as an AutoCAD command.
This is only true if you call the function from the AutoCAD command line.
If you call the function from within another function you must precede it with C:


It's a good idea to leave all variables as global whilst you are writing your
program so that you can check their values during debugging.