Calling sub function syntax

Calling sub function syntax

Anonymous
Not applicable
6,420 Views
7 Replies
Message 1 of 8

Calling sub function syntax

Anonymous
Not applicable

I've never written a lisp with a subfunction within it before, please can someone check the following syntax is correct?

 

I know it's not very ambitious to say the least but if I can get the basics right then subsequent lisps might stand a chance

 

 

(defun c:aaa ()
    (alert "this is aaa")
   (bbb)
      (alert "this is aaa again")
  (princ)
  )


(defun c:bbb ()
    (alert "this is bbb")
  )

 

0 Likes
Accepted solutions (1)
6,421 Views
7 Replies
Replies (7)
Message 2 of 8

ВeekeeCZ
Consultant
Consultant
Accepted solution

If you're about calling your bbb sub multiply just from main c:aaa routine, then something like this. Put it inside of c:aaa and localize bbb.

 

Spoiler
(defun c:aaa ( / bbb)

  (defun bbb ()
    (alert "this is bbb")
  )

  ; main program
  
  (alert "this is aaa") 
  (bbb)
  (alert "this is aaa again")
  (princ)
)

 

But if you about to call bbb from other routines as well (e.g. c:ccc), then use this:

 

Spoiler
(defun c:aaa ()
  (alert "this is aaa") 
  (bbb)
  (alert "this is aaa again")
  (princ)
)

(defun c:ccc ()
  (alert "this is ccc") 
  (bbb)
  (alert "this is ccc again")
  (princ)
)

(defun bbb ()
    (alert "this is bbb")
)

 

And if you want to call bbb separately as command as well, use c: prefix. But usually you don't...

 

Spoiler
(defun c:aaa ()
  (alert "this is aaa") 
  (c:bbb)
  (alert "this is aaa again")
  (princ)
)

(defun c:bbb ()
    (alert "this is c:bbb")
)

 

Message 3 of 8

Anonymous
Not applicable

Thanks ever so for that.

 

I'll definitely be keeping this page in my Favourites to refer to again !

0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant

You can either define bbb without the C: prefix, as already suggested, or you can use it with the C: prefix:

 

(defun c:aaa ()
  (alert "this is aaa")
  (c:bbb)
  (alert "this is aaa again")
  (princ)
)

(defun c:bbb ()
  (alert "this is bbb")
)

 

The advantage of that is that, if you want to, you can enter bbb itself as a command name.  If it's defined without the C: prefix, to use it on its own you need to call it with the parentheses:

 

Command:  (bbb)

Kent Cooper, AIA
0 Likes
Message 5 of 8

Anonymous
Not applicable

Thanks for that Kent, it's all clear to me now. My mistake was that I referred to other people lisps, but couldn’t work out why my little test lisp failed. All the other folks lisps don’t have the c: at the beginning of their sub functions & I didn’t notice. I’m so used to starting a function these days with:

 

(defun c:my_func ()

 

that it’s become automatic.


I couldn’t find much about it with a Google either.

0 Likes
Message 6 of 8

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

...


I couldn’t find much about it with a Google either.


How about help! 🙂

0 Likes
Message 7 of 8

doni49
Mentor
Mentor

When writing a routine that I think might be useful to be called from another routine, but also one that I want to use as a command unto itself, I usually write it WITHOUT the c: (what I refer to as a performing function) and then write a function that gathers the user input and then passes it to the other function (aka a gathering function).  For example:

 

;;This is how a routine might normally be written as a command.
;;
(defun c:mycommand ( / ents var1 var2)
  (setq ents(ssget))
  (setq var1 (getpoint "Point1:  "))
  (setq var2 (getpoint "Point2:  "))
  (command "move" ents "" var1 var2)
)

;;now if I want to make it so that a different routine can gather the user input
;;(and probably do other things too) then perform the tasks, I would break it 
;;into a performing function and a gathering function.

;;this is the gathering function.
(defun c:mycommand( / ents var1 var2)
  (setq ents(ssget))
  (setq var1 (getpoint "Point1:  "))
  (setq var2 (getpoint "Point2:  "))
  (mycommand ents var1 var2) ;; <--it might look like it's calling itself.  
                                                  ;;     But it's calling the performing function
)
;;this is the performing function
(defun mycommand (ents var1 var2)
  (command "Move" ents "" var1 var2)
)

 This is just one example as to why both styles are useful.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes
Message 8 of 8

scot-65
Advisor
Advisor
Next.
Try this as a function with a call:
(defun c:AAA ()
(alert "this is a")
(bbb "This is bbb-1")
(alert "this is a")
(bbb "This is bbb-2")
(alert "this is a")
(princ)
)

(defun bbb ( a / )
(alert a)
)

bbb cannot be a command in this instance because
of the call (item in front of the forward slash).

Next.
Function returns a value:
(defun c:AAA ()
(alert (bbb))
)

(defun bbb ()
"This is bbb-3"
)

Another way to do this:
(defun bbb ()
(setq b "this is bbb-4")
(princ)
b
)

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes