One basic question

One basic question

Anonymous
Not applicable
1,532 Views
10 Replies
Message 1 of 11

One basic question

Anonymous
Not applicable
Hi,
I have one basic question.
I am new to lisp, I read/used many lisp programs in which I noticed that there are some functions without C: prefix and they are used in main lisp.
I read about it how they are created and used but still dont understand how exactly to use it.
Example:
(defun C:ABC (XYZ /)
(XYZ)
)

(defun XYZ ()
..................
..................
..................
)
are this can be directly used in main lisp ABC or need another functon to put the final value of XYZ in main lisp ABC?
0 Likes
Accepted solutions (1)
1,533 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor
0 Likes
Message 3 of 11

Anonymous
Not applicable
Thank You...
0 Likes
Message 4 of 11

john.uhden
Mentor
Mentor
Accepted solution

I haven't read Mr. Rampage's article, but basically there are two (2) types of functions...

 

1.  That is prefixed with C: to become an AutoLisp command that can typed into the command line and run just like any AutoCAD command,

2.  That serves as a home-grown AutoLisp function to accept arguments and perform some specific task.  They can be called from within AutoLisp code without the need for the command function.  Functions are almost always intended to return a value, just like (+ 1 2) returns 3.

 

Example:

 

(defun average (values)

  (/ (apply '+ values)(length values))

)

 

(defun C:Average ( / value values)

  (while (setq value (getreal "\nEnter number to average: "))

    (setq values (cons value values))

  )

  (princ (strcat "\nAverage = " (rtos (average values) 2 3)))

  (princ)

)

 

So while the c:average function is a helpful way to take the user's input and call the average function, he can also use it from the command line as in...

Command: (average (list 1.1 3.2 2.6 4.7 pi))

John F. Uhden

Message 5 of 11

Anonymous
Not applicable
Thanks john,
This is more clear & easy to understand, the way you explain.
0 Likes
Message 6 of 11

Anonymous
Not applicable
Hi John,
If I wrote this Average function in one lisp routine, Can I use this in other lisp without writing it in that lisp or I have to copy this in other lisp too? Is it globally used or is local for particular lisp only?

Thanks...
0 Likes
Message 7 of 11

pbejse
Mentor
Mentor

@Anonymous.vip wrote:
Hi John,
If I wrote this Average function in one lisp routine, Can I use this in other lisp without writing it in that lisp or I have to copy this in other lisp too? Is it globally used or is local for particular lisp only?

Thanks...

 

 The way that John's post is constructed, average subfunction is GLOBAL,  but only on the current drawing session


(defun average (values)

  (/ (apply '+ values)(length values))

)

 

(defun C:Average ( / value values)

  (while (setq value (getreal "\nEnter number to average: "))

    (setq values (cons value values))

  )

  (princ (strcat "\nAverage = " (rtos (average values) 2 3)))

  (princ)

)


 

To declare the function as LOCAL, you must include the function on the argument list after this symbol "/"

 

 

(defun C:Average ( / average value values)
(defun average (values)
  (/ (apply '+ values)(length values))
)
       
  (while (setq value (getreal "\nEnter number to average: "))
    (setq values (cons value values))
  )
  (princ (strcat "\nAverage = " (rtos (average values) 2 3)))
  (princ)
)

 

 

Makes me wonder if you even read thru the link i posted Smiley Very Happy

 

Cheers

 

 

 

0 Likes
Message 8 of 11

Anonymous
Not applicable
Sorry I think my question was confusing.

As you said these sub functions can be use in current drawing session I got the answer.
"I just wanted to know can I use this sub functions again in another lisp in another drawing session just by calling "Average"?
0 Likes
Message 9 of 11

pbejse
Mentor
Mentor

@Anonymous.vip wrote:

"I just wanted to know can I use this sub functions again in another lisp in another drawing session just by calling "Average"?

Why of course, you can. 

 

Loading Programs Automatically

 

One way.Say.

 

(defun C:Average ( / value values)
(or average (load "M:\\InThisForest\\Otherlisp.lsp"))
(while (setq value (getreal "\nEnter number to average: ")) (setq values (cons value values)) ) (princ (strcat "\nAverage = " (rtos (average values) 2 3))) (princ) )

Side note: It's important that you set proper naming convention for your functions, average could be variable 

 

I'll post a link for a good read on naming conventions.

 

0 Likes
Message 10 of 11

john.uhden
Mentor
Mentor

@pbejse's responses are dead on, as usual.

 

I put functions that are used by other various functions in a, let's say, "BASE.LSP" file which I load within acaddoc.lsp which gets automatically loaded with every drawing new or open.  That way they are always available without needing to redefine them.  Or just define your functions in acaddoc.lsp.

 

I lied.  What I really do is to compile the "BASE.LSP" file into BASE.FAS so that no one else in a networked environment can alter it.  It should be obvious, but then there's a line in acaddoc.lsp that reads...

(load "base.fas")

 

The only problem with global functions is that they take up a little memory and might not even be used in a drawing session.  But unless the functions are collectively voluminous it's not significant.

John F. Uhden

0 Likes
Message 11 of 11

Anonymous
Not applicable
Thanks both of you...
It helps me a lot...
0 Likes