0% found this document useful (0 votes)
154 views

The Define Function (Defun) - AfraLISP PDF

Uploaded by

sixramesh123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

The Define Function (Defun) - AfraLISP PDF

Uploaded by

sixramesh123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Home

Contact
Site Map

Custom Search Search

AutoLISP
Beginners' Tutorials
Intermediate Tutorials
Advanced Tutorials
Application Tutorials
Tips'n'Tricks
Visual LISP
Beginners' Tutorials
Intermediate Tutorials
DCL
Beginners' Tutorials
Intermediate Tutorials
VBA
VBA Tutorials
VBA Applications
Tips'nTricks
AutoCAD
Customization Tutorials
Tips'n'Tricks
Reference
AutoLISP Functions
Visual LISP Sample Files
DXF Group Codes
The AutoCAD APIs
DCL Tile Attributes
AutoLISP DCL Functions
System Variables
AutoCAD Object Model
Sin and Cos Functions
VLAX Enumeration
Download
Forum

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))
(princ)
);defun

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))

(princ)

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:

(C:DDSTEEL)

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.

AutoLISP
AutoLISP Beginners' Tutorials
AutoLISP Intermediate Tutorials
AutoLISP Advanced Tutorials
AutoLISP Application Tutorials
AutoLISP Tips'n'Tricks

AfraLISP Archive
‘Hey, what's happened to AfraLISP?’ If you've visited our site before, you'll notice some big changes. We're currently
revamping the entire site to bring you updated tutorials and a better user experience. However, if there's something you can't
find, the AfraLISP Archive contains a full copy of the original site as originally created by Kenny Ramage.
AutoLISP Learning Resources
Lee Mac - AutoLISP Tutorials and Programmes
Draftsperson.net - AutoLISP Tutorials
Jeffery P Sanders - AutoLISP Tutorials
Ron Leigh - AutoLISP Tutorials
Pixel Graphics Inc. - AutoLISP Tutorials
A'CAD Solutions - AutoLISP Tutorial
CAD Digest - AutoLISP Tutorials

Online Books
The ABC's of AutoLISP
The Visual LISP Developer's Bible

AutoLISP Forums
CADTutor
Autodesk Discussion Groups
Autodesk User Group International (AUGI)
The Swamp

122 Tutorials and reference documents published at AfraLISP so far.

Back to top
Home
Cared for by David Watson © 2018

You might also like