PDA

View Full Version : DEFUN's "arguments"


BoKirra
2009-03-16, 05:18 AM
Hi ALL,

In AutoCAD HELP, there is an explanation about the element "arguments" in DEFUN function.


(defun sym ([arguments] [/ variables...]) expr...)


Can anyone please give an example from the real world and explain it's use?
Because I haven't used it before & don't know how (and when) to apply it to the routine.

Again, your helps are much appreciated.

steve.ashton
2009-03-16, 06:50 AM
You can use the arguments as a way of passing information to a lisp subroutine. This way you can write reusable routines. Each time a routine is called you can pass it a different number or selection set.

I have a bill of materials routine that has a move-up routine (defun move_up (ss / ...) to make room for for additional lines. I can then call it various times. (move_up ss_tot) moves up the block containing the totals, followed by (move_up ss_bolt) to move up blocks that contain bolt information. The same routine can process differnet selection sets.

Creating reusable subroutines can reduce coding and make the program easier to follow.

tedg
2009-03-16, 01:21 PM
Hi ALL,

In AutoCAD HELP, there is an explanation about the element "arguments" in DEFUN function.


(defun sym ([arguments] [/ variables...]) expr...)
Can anyone please give an example from the real world and explain it's use?
Because I haven't used it before & don't know how (and when) to apply it to the routine.

Again, your helps are much appreciated.
Also, you could look into this thread (http://forums.augi.com/showthread.php?t=42122) among others. They help to break down and explain the components of a routine.

Use the "search" feature of the forums, it's very helpful.

rkmcswain
2009-03-16, 02:50 PM
Can anyone please give an example from the real world and explain it's use?


Example. This function converts MPH to FPS. The "x" is the argument. Notice that all this really does is multiply "x" by 1.46666666. Below there are examples of calling this function and the return values.


(defun foo (x)
(* 1.46666666 x)
)

(foo 60)
;; returns 88

(foo 75)
;; returns 110

andrea.andreetti
2009-03-16, 07:08 PM
you can use any type of arguments.

eg:


(vl-load-com)
(DEFUN returnvars ( var )
(alert (strcat
"var name: " (vl-princ-to-string var)
"\nvar value: " (vl-princ-to-string (eval var))
"\nvar Type: " (vl-princ-to-string (type (eval var))))
)
)

;;TESTS
(setq start 32)
(returnvars 'start)

(setq start "hello")
(returnvars 'start)

(setq start (list "a" "b" 1 2))
(returnvars 'start)

(setq start 32.4)
(returnvars 'start)

(returnvars 'returnvars)



or use multiple arguments

eg:



(DEFUN returnvars ( var1 var2 )
(alert (strcat
"var1 name: " (vl-princ-to-string var1)
"\nvar1 name: " (vl-princ-to-string var2)
)
)
)
(returnvars "test" 44)

Terry Cadd
2009-03-17, 01:58 AM
Hi BoKirra,
Using 'arguments' is a way to pass values to an AutoLISP function instead of asking for the values within the function.

For example a function to multiply A x B:

(defun Multiply (A B / )
(* A B)
);defun Multiply

Syntax example: (Multiply 21 37) = 777
Note: This is only an example, and would otherwise be written as (* A B).

Another type of function is the command type which can be run from the command line.
For this example you would need to ask for the values from the user:

(defun c:Multiply (/ A B)
(setq A (getreal "\nEnter value for A: "))
(setq B (getreal "\nEnter value for B: "))
(princ "\n= ") (princ (* A B))
(princ)
);defun c:Multiply

Syntax example: Multiply
Enter value for A: 21
Enter value for B: 37
= 777.0

Here is a short introduction to AutoLISP that covers a little about 'arguments' and 'variables'.
http://web2.airmail.net/terrycad/Tutorials/MyDialogs.htm#IntroAutoLISP

BoKirra
2009-03-18, 11:31 PM
Hi BoKirra,
Using 'arguments' is a way to pass values to an AutoLISP function instead of asking for the values within the function.

For example a function to multiply A x B:

(defun Multiply (A B / )
(* A B)
);defun Multiply

Syntax example: (Multiply 21 37) = 777
Note: This is only an example, and would otherwise be written as (* A B).

Another type of function is the command type which can be run from the command line.
For this example you would need to ask for the values from the user:

(defun c:Multiply (/ A B)
(setq A (getreal "\nEnter value for A: "))
(setq B (getreal "\nEnter value for B: "))
(princ "\n= ") (princ (* A B))
(princ)
);defun c:Multiply

Syntax example: Multiply
Enter value for A: 21
Enter value for B: 37
= 777.0

Here is a short introduction to AutoLISP that covers a little about 'arguments' and 'variables'.
http://web2.airmail.net/terrycad/Tutorials/MyDialogs.htm#IntroAutoLISP

Thanks for your help, Terry.
And thanks to ALL.
Now what I need is - more practices. :Puffy: