The Define Function (Defun) - AfraLISP PDF
The Define Function (Defun) - AfraLISP PDF
Contact
Site Map
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
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 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.
(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.
Now, at the AutoCAD command line, assign the variables ANG1 and ANG2 to values other than those used by the TEST
function :
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.
(* PI (/a 180.0))
)
To use this, we must pass the value of the argument to the function :
(DTR 90.0)
(setq a (* (* r r) PI))
(princ)
Now try it :
It should return :
Area = 1809.6
(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
Back to top
Home
Cared for by David Watson © 2018