Auto/VisualLISP 101 – 2

In our first article in this series, we explained that LISP is a List Processing language. This means that all coding you write in this language has to be contained in a list. The list is created by enclosing your phrase in parentheses (). For example, to add two plus one in lisp you would write (+ 2 1).

In this exercise, we are going to start building our function as described in the first article.

In AutoLISP, we are actually going to be DEFining a FUNction. Therefore, we will use the following:

(DEFUN C:CTS ()

)

Now the tools we use in the LISP language are also called functions! This can be a little confusing so we are going to refer to them simply in their proper coding context (what they are used for).

The LISP method named DEFUN defines the function called “CTS”. The “C:” before the function name simply allows us to call the function from the command line. Otherwise, we would have to write the following to the command line “(CTS)” to call the function once it is loaded.

Also note that currently there is an empty parentheses pair after the function name. This is used for variable declaration. A variable is simply a symbol that stores data in memory. To create a variable and store the data we use a method called SETQ. A symbol can be any alphanumeric character or mixture thereof. Any expression can follow but you cannot assign more than one expression to a value. For example, in our sample above of two plus one, we could have written like this:

(SETQ a 2)

(SETQ b 1)

(+ a b)

Using SETQ, I have set the symbol “a” to value of “2” and “b” to the value of “1”.

There are a few different types of variables you can write. You can write local variables which mean that they will only exist during the length of the function (Only within the DEFUN list). You define the variables as local by adding them to that empty parentheses pair above following a forward slash. For example:

(DEFUN C:CTS (/ a b)

     (SETQ a 2)

     (SETQ b 1)

     (+ a b)

)

You can also define variables as global which means that will exist for the life of the current drawing’s session. This can be useful at times but is considered a bad practice since you aren’t really ‘cleaning’ up after yourself and eating up memory unnecessarily. You define the variables locally simply by leaving them out of the list above.

Lastly, you can create a function which will require variables to be passed INTO it at the time of being called. However, we won’t talk about this in this series.

In our next article in this series, we are going to look at the LISP method called SSGET.

One comment

  1. […] our last article we looked at how to create a function and create variables. Now let’s look at getting some […]