Function names as variables

Function names as variables

Anonymous
Not applicable
327 Views
14 Replies
Message 1 of 15

Function names as variables

Anonymous
Not applicable
I'd appreciate an opinion on the topic of function names.
I've picked up some vague ideas I'd like to be more sure
of.

1. Is a function name actually a global variable unless it's
declared local somehow?

2. If so, would it best be declared local, and is that easily
done? How?

3. (Alternatively, if a function name is left global, then when a
program ends, presumably this function should be set nil?)

I've wondering if I've rarely seen function names handled like
variables because either:
- I've seen mostly sample routines, in which every tedious
propriety isn't followed
- or it it's not that important

Joe "Blow" Funk
0 Likes
328 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable
Still wondering.

I am using VLISP R2000, not AutoLISP.

I hope the quoted authors below pardon me for taking
their names in vain.

After a search of the .customization ng at the AutoDesk site:

Re. VLISP & using nested functions.

(defun function1 (/ function2)
(defun function2 ...)
)

Vladimir Nesterovsky: (re. VLISP)
...if used in library code that have to be the most efficient,
we must move those nested defun definitions out of the function,
thus polluting the name space with functions we'll never call
from outside.

Then how/where should/could you declare a function local?
Is it preferable/necessary to keep the function names global in VLISP?

Re. General Good Practices:

Michael Weaver:
It is highly recommend you use only local variables, local functions
when applicable, and prefix all your public functions with unique
names that you can be assured do not duplicate any AutoCAD
variable/function names or third party ones. Best bet is to use a
common prefix for all your public functions/variables.

Thoroughly confused, but not really sure I should worry.

townfool
0 Likes
Message 3 of 15

Anonymous
Not applicable
Joe,

You probably know more than I about AutoLISP, and everything you know
about VLISP is more that I know about it, but let's just throw
something out here for the experts to correct us on. 🙂

This is the way I see it:

(defun C:FUNC (/) a global function - may be called as (c:func) or
as a command

(defun (FUNC (/) a local function - may be called as (func)

(defun C:FUNC (/ func2) or (defun FUNC (/ func2)
(defun func2 (/) a nested function that cannot be called except
from within FUNC as (func2). I would also think that func2 would
return to nil when the program finished, whereas C:FUNC or FUNC would
remain in the drawing memory space once loaded.

(defun C:FUNC (/ var1 var2) or (defun FUNC (/ var1 var2)
(setq var1 ......) a local variable that would return to nil
(setq var2 ......) a local variable that would return to nil
(setq var3 ......) a global variable that would remain in the
drawing memory space.

(defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4)
var2 is a variable [argument] that must be passed with C:FUNKY or
FUNKY when calling it, as:

(c:funky var2) or (funky var2), although the names don't seem to
have to match. a function such as:
(defun C:FUNKY (x / var4) or (defun FUNKY (x / var4), or even
(defun C:FUNKY (nil / var4) or (defun FUNKY (nil / var4) would work
when called as above.

Well, let's see how all that horse pucky flies.
--
Dave D

Joe Funk wrote in article
<8b6nmo$r8r40@adesknews2.autodesk.com>...
> Still wondering.
>
> I am using VLISP R2000, not AutoLISP.
<....>
0 Likes
Message 4 of 15

Anonymous
Not applicable
David;
Yer a brave soul. You lent a bit of credence to my
last attempted booby-trap:
"how to use UNDO properly".
I guess I'm wondering what's left if nested functions
are not so good.

"David Doane" wrote in message
news:01bf92eb$7a023c80$36876ace@lms-1...
> Joe,
>
> You probably know more than I about AutoLISP, and everything you know
> about VLISP is more that I know about it, but let's just throw
> something out here for the experts to correct us on. 🙂
>
> This is the way I see it:
>
> (defun C:FUNC (/) a global function - may be called as (c:func) or
> as a command
>
> (defun (FUNC (/) a local function - may be called as (func)
>
> (defun C:FUNC (/ func2) or (defun FUNC (/ func2)
> (defun func2 (/) a nested function that cannot be called except
> from within FUNC as (func2). I would also think that func2 would
> return to nil when the program finished, whereas C:FUNC or FUNC would
> remain in the drawing memory space once loaded.
>
> (defun C:FUNC (/ var1 var2) or (defun FUNC (/ var1 var2)
> (setq var1 ......) a local variable that would return to nil
> (setq var2 ......) a local variable that would return to nil
> (setq var3 ......) a global variable that would remain in the
> drawing memory space.
>
> (defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4)
> var2 is a variable [argument] that must be passed with C:FUNKY or
> FUNKY when calling it, as:
>
> (c:funky var2) or (funky var2), although the names don't seem to
> have to match. a function such as:
> (defun C:FUNKY (x / var4) or (defun FUNKY (x / var4), or even
> (defun C:FUNKY (nil / var4) or (defun FUNKY (nil / var4) would work
> when called as above.
>
> Well, let's see how all that horse pucky flies.
> --
> Dave D
>
>
>
> Joe Funk wrote in article
> <8b6nmo$r8r40@adesknews2.autodesk.com>...
> > Still wondering.
> >
> > I am using VLISP R2000, not AutoLISP.
> <....>
>
0 Likes
Message 5 of 15

Anonymous
Not applicable
Hi David, Joe,

Vladimir points out in one of the quoted threads that, while nested defuns
may have made some sense in AutoLisp, they actually degrade performance in
Vlisp.

I started using nested defuns in AutoLisp several years ago to minimize
collisions with other functions. Now, with Vlisp, I suppose the thing to do
is leave the defuns global and compile to a vlx file. By exporting only
those functions that are required to be global; i.e., available outside the
vlx, the rest of the functions remain local.

David, I've added comments to your examples below:

"David Doane" wrote in message
news:01bf92eb$7a023c80$36876ace@lms-1...
> Joe,
>
> You probably know more than I about AutoLISP, and everything you know
> about VLISP is more that I know about it, but let's just throw
> something out here for the experts to correct us on. 🙂
>
> This is the way I see it:
>
> (defun C:FUNC (/) a global function - may be called as (c:func) or
> as a command

Agreed

>
> (defun (FUNC (/) a local function - may be called as (func)

Most lisp object snaps are defined this way.

>
> (defun C:FUNC (/ func2) or (defun FUNC (/ func2)
> (defun func2 (/) a nested function that cannot be called except
> from within FUNC as (func2). I would also think that func2 would
> return to nil when the program finished, whereas C:FUNC or FUNC would
> remain in the drawing memory space once loaded.
>

Yes, func2 is declared as local and ceases to exist once C:FUNC or FUNC
completes.

> (defun C:FUNC (/ var1 var2) or (defun FUNC (/ var1 var2)
> (setq var1 ......) a local variable that would return to nil
> (setq var2 ......) a local variable that would return to nil
> (setq var3 ......) a global variable that would remain in the
> drawing memory space.

You've got it.

>
> (defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4)
> var2 is a variable [argument] that must be passed with C:FUNKY or
> FUNKY when calling it, as:
>

Using functions that require arguments, as you've shown here, can greatly
simplify your code. I often use no global vars between my subroutines,
instead passing arguments and returning values. Often my arguments and
return values consist of lists of data. One of the challenges that I find
in using VBA is the difficulty in passing and returning complex argument
lists.

> (c:funky var2) or (funky var2), although the names don't seem to
> have to match. a function such as:
> (defun C:FUNKY (x / var4) or (defun FUNKY (x / var4), or even
> (defun C:FUNKY (nil / var4) or (defun FUNKY (nil / var4) would work
> when called as above.

When I try to load a function with nil as a declared argument, I get an
error message (A2K). A delcared argument must follow AutoLisp's symbol
naming conventions. If nil were allowed as a declared argument it would
result in changing nil's value to something other than nil, resulting in
bugs that would be very difficult to find.

>
> Well, let's see how all that horse pucky flies.
> --
> Dave D
>
>
>
> Joe Funk wrote in article
> <8b6nmo$r8r40@adesknews2.autodesk.com>...
> > Still wondering.
> >
> > I am using VLISP R2000, not AutoLISP.
> <....>
>
0 Likes
Message 6 of 15

Anonymous
Not applicable
Michael & David;

Thanks. Obviously I've just been diddling with AutoLISP/VLISP
for the last couple of years. I don't even know some of the basic
stuff.

David Doane wrote
>> (defun (FUNC (/) a local function - may be called as (func)

Michael Weaver wrote
>Most lisp object snaps are defined this way.

I should at least be aware of it, so I'll look for examples. I
don't understand the construction right off.
0 Likes
Message 7 of 15

Anonymous
Not applicable
Michael;
Thanks.

"Michael Weaver" wrote in message
news:8b7156$15u2@adesknews2.autodesk.com...
> Hi David, Joe,
...
> Now, with Vlisp, I suppose the thing to do is leave the defuns global
>and compile to a vlx file. By exporting only those functions that are
>required to be global; i.e., available outside the vlx, the rest of the
>functions remain local.
0 Likes
Message 8 of 15

Anonymous
Not applicable
Michael and Joe,

Thanks for your evaluation, Michael. See below for correction:

Michael Weaver wrote in article
<8b7156$15u2@adesknews2.autodesk.com>...
> Hi David, Joe,
<....>
> > (defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4)
> > var2 is a variable [argument] that must be passed with C:FUNKY
or
> > FUNKY when calling it, as:
<....>
> > (c:funky var2) or (funky var2), although the names don't seem to
> > have to match. a function such as:
> > (defun C:FUNKY (x / var4) or (defun FUNKY (x / var4), or even
> > (defun C:FUNKY (nil / var4) or (defun FUNKY (nil / var4) would
work
> > when called as above.
>
> When I try to load a function with nil as a declared argument, I
get an
> error message (A2K). A delcared argument must follow AutoLisp's
symbol
> naming conventions. If nil were allowed as a declared argument it
would
> result in changing nil's value to something other than nil,
resulting in
> bugs that would be very difficult to find.
<....>

Oops! Got it backwards. 😞 It should be:

(defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4)
var2 is a variable [argument] that must be passed *TO* C:FUNKY or
FUNKY when calling it, as:
(c:funky var2) or (funky var2), although the names don't seem to
have to match. a function such as:
(defun C:FUNKY (var2 / var4) or (defun FUNKY (var2 / var4) may be
called as

(c:funky x) or (funky x), and x would be set to the value of var2,
or what ever was passed to it.
(c:funky nil) or (funky nil), would set to the value of var2, or
what ever was passed to it to nil, within C:FUNKY or FUNKY only. It
is commonly used as a true/false test within the called program, such
as:

(if (/= var2 nil)
(do something constructive, with or without var2)
(do something else, or nothing at all)
)

What would you use it for?? Hmmm. Perhaps like so:

(if (/= var2 nil)
(draw a polygon counter clockwise)
(draw a polygon clockwise by reversing the points/pointlist)
)

Do that make more sense??
--
Dave D
0 Likes
Message 9 of 15

Anonymous
Not applicable
Be aware there is a typo here:

> (defun (FUNC (/) a local function - may be called as (func)

should be (no paren in front of FUNC):

(defun FUNC (/) a local function - may be called as (func)

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)

Joe Funk wrote in message
news:8b72dj$14c4@adesknews2.autodesk.com...
> Michael & David;
>
> Thanks. Obviously I've just been diddling with AutoLISP/VLISP
> for the last couple of years. I don't even know some of the basic
> stuff.
>
> David Doane wrote
> >> (defun (FUNC (/) a local function - may be called as (func)
>
> Michael Weaver wrote
> >Most lisp object snaps are defined this way.
>
> I should at least be aware of it, so I'll look for examples. I
> don't understand the construction right off.
>
>
>
0 Likes
Message 10 of 15

Anonymous
Not applicable
Thanks.
I thought maybe it was a rare form I'd have to look up.

R. Robert Bell wrote
> Be aware there is a typo here:
>
> > (defun (FUNC (/) a local function - may be called as (func)
>
> should be (no paren in front of FUNC):
>
> (defun FUNC (/) a local function - may be called as (func)
>
>
> --
> R. Robert Bell, MCSE
> Network Administrator (or, Modern-day Wizard)
>
0 Likes
Message 11 of 15

Anonymous
Not applicable
I like it.

David Doane wrote

> Michael and Joe,
...

> What would you use it for?? Hmmm. Perhaps like so:
>
> (if (/= var2 nil)
> (draw a polygon counter clockwise)
> (draw a polygon clockwise by reversing the points/pointlist)
> )
>
> Do that make more sense??
> --
> Dave D
0 Likes
Message 12 of 15

Anonymous
Not applicable
Picky, picky.

You are of course correct, and typos are inexcusable when giving an
example.

I am surprised that there is not more constructive criticism, though.
I was just expressing the way I see them derived from the way I have
seen them used - not from any expert knowledge.
--
Dave D

R. Robert Bell wrote in article
<8b86al$48318@adesknews2.autodesk.com>...
> Be aware there is a typo here:
>
> > (defun (FUNC (/) a local function - may be called as (func)
>
> should be (no paren in front of FUNC):
>
> (defun FUNC (/) a local function - may be called as (func)
>
>
> --
> R. Robert Bell, MCSE
> Network Administrator (or, Modern-day Wizard)
0 Likes
Message 13 of 15

Anonymous
Not applicable
Yes!

"Joe Funk" wrote in message
news:8b89k1$48327@adesknews2.autodesk.com...
> I like it.
>
> David Doane wrote
>
> > Michael and Joe,
> ...
>
> > What would you use it for?? Hmmm. Perhaps like so:
> >
> > (if (/= var2 nil)
> > (draw a polygon counter clockwise)
> > (draw a polygon clockwise by reversing the points/pointlist)
> > )
> >
> > Do that make more sense??
> > --
> > Dave D
>
>
0 Likes
Message 14 of 15

Anonymous
Not applicable
Of course, since you are looking at the symbol anyway, this works in a
slightly cleaner fashion:

(if var2
(draw a polygon counter clockwise)
(draw a polygon clockwise by reversing the points/pointlist)
)

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)

Michael Weaver wrote in message
news:8b8c2d$48334@adesknews2.autodesk.com...
> Yes!
>
> "Joe Funk" wrote in message
> news:8b89k1$48327@adesknews2.autodesk.com...
> > I like it.
> >
> > David Doane wrote
> >
> > > Michael and Joe,
> > ...
> >
> > > What would you use it for?? Hmmm. Perhaps like so:
> > >
> > > (if (/= var2 nil)
> > > (draw a polygon counter clockwise)
> > > (draw a polygon clockwise by reversing the points/pointlist)
> > > )
> > >
> > > Do that make more sense??
> > > --
> > > Dave D
> >
> >
>
>
0 Likes
Message 15 of 15

Anonymous
Not applicable
Not much to add here really. You pretty much covered all the bases. The only
other usage that I haven't seen mentioned yet is the one I use for my
"toolbox" routines.

(defun I:MyTool () ; this way I know the function is one of my toolbox
routines.

Usage: (setq Blah (I:MyTool))

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)

David Doane wrote in message
news:01bf935b$430b8a40$c757d2d0@lms-1...
> Picky, picky.
>
> You are of course correct, and typos are inexcusable when giving an
> example.
>
> I am surprised that there is not more constructive criticism, though.
> I was just expressing the way I see them derived from the way I have
> seen them used - not from any expert knowledge.
> --
> Dave D
>
> R. Robert Bell wrote in article
> <8b86al$48318@adesknews2.autodesk.com>...
> > Be aware there is a typo here:
> >
> > > (defun (FUNC (/) a local function - may be called as (func)
> >
> > should be (no paren in front of FUNC):
> >
> > (defun FUNC (/) a local function - may be called as (func)
> >
> >
> > --
> > R. Robert Bell, MCSE
> > Network Administrator (or, Modern-day Wizard)
>
0 Likes