Jump to content

Recommended Posts

Posted

Hi All,

 

I am new in LISP and going to ask simple questions from now on!

 

I wrote:

 

(defun C:test (/ a)

(setq a 1)

 

)

and once I am running it in AutoCAD I get this:

 

Command: test

1

Why "1" printed? (I know the variable "a" has "1" in it but why should I have that after I run the program? If I want to see that I can ask later!)

how can I get rid of it?

 

 

Cheers

 

Ali

Posted

Because it print the returned value of that function - that it, the last evaluated expression. To avoid that always use PRINC statement as last line in your function definitions.

 

(defun c:test( / a)
(setq a 1)
(princ)
)

 

Regards,

Posted
If I want to see that I can ask later!

 

I’m afraid that this isn’t true, since you declared the variable a as local, it will not be visible from outside the C:TEST function’s name space.

 

Regards,

Posted
Because it print the returned value of that function - that it, the last evaluated expression. To avoid that always use PRINC statement as last line in your function definitions.

 

(defun c:test( / a)
(setq a 1)
(princ)
)

Regards,

 

Thanks a lot,

 

what does it mean "returned value"? can you please explain it to me?

I know the value has changed to "1" but I don't understand having a "return" of anything unless I ask for it!

 

Cheers

 

Ali

Posted

In AutoLISP a function will return ALWAYS the value of last evaluated expression. Is not as in Visual Basic where you must assign the returned value to function name (so can control whether a function will return something or not); also the procedure (that it, function that doesn’t return) concept isn’t available.

To avoid having a function to return something is required to have as last expression a built-in statement that doesn’t return (i.e. PRINC). Check in help for each AutoLISP function to see what is the returned value.

 

Try to run few times the below code to understand his behavior:

 

(defun c:f1()
(if (not theTestValue) (setq theTestValue 0))
(if (= (rem (setq theTestValue (1+ theTestValue)) 3) 0)
 "X"
 theTestValue
)
)

 

Regards,

Posted
In AutoLISP a function will return ALWAYS the value of last evaluated expression. Is not as in Visual Basic where you must assign the returned value to function name (so can control whether a function will return something or not); also the procedure (that it, function that doesn’t return) concept isn’t available.

To avoid having a function to return something is required to have as last expression a built-in statement that doesn’t return (i.e. PRINC). Check in help for each AutoLISP function to see what is the returned value.

 

Try to run few times the below code to understand his behavior:

 

(defun c:f1()
(if (not theTestValue) (setq theTestValue 0))
(if (= (rem (setq theTestValue (1+ theTestValue)) 3) 0)
 "X"
 theTestValue
)
)

Regards,

 

 

Thanks mate,

 

you are the man

 

That code is an advance code for me. I start with this one and for some reasons it doesn't work!

can you please help me with this:

 

code:

;Test

(defun c:prac3 (/ a b)

(setq a (getstring "\nChoose one [H/E/A/C]:"))

 

(if (= a "H") ((setq b 2)))

 

(print a)

(print b)

(princ)

 

)

 

and I get:

 

Command: prac3

Choose one [H/E/A/C]:H

; error: bad function: 2

 

Cheers

 

Ali

Posted

You welcome!

Regarding that error, please take care that you have an extra set of parenthesis on your code:

 

(defun c:prac3 (/ a b)
(setq a (getstring "\nChoose one [H/E/A/C]:"))

 (if (= a "H") [color=red]([/color](setq b 2)[color=red])[/color])

 (print a)
 (print b)
 (princ)
)

 

Regards,

Posted

By the way, for the case when you want to constrain your user to select only from a predefined set of options may use GETKWORD instead of GETSTRING; use INITGET to define the constrain:

 

(initget “H E A C” 1)
(setq a (getkword [color=black]"\nChoose one [H/E/A/C]:"))[/color]

 

This way will alow also the user to enter both "H" and "h" as valid input.

 

Regards,

Posted

Thanks again,

 

I wrote:

(defun c:prac3 (/ a b)

(setq a (getstring "\nChoose one [H/E/A/C]:"))

 

(if (= a H) (setq b 2))

 

(print a)

(print b)

(princ)

 

)

 

and I get:

Command: PRAC3

Choose one [H/E/A/C]:H

"H"

nil

 

why 'nil'!!!

it should be 2!

 

can you please guide me through? I know I am pain now. sorry....

Posted

got it !!!!

 

thanks a lot.

Posted

Sound good! You're welcome!

 

For beginning can be a good practice to use global variables instead of local - this is to allow debug listing of assignments on prompter:

 

(defun c:f1()
(setq a "1")
(princ)
)

Command: !a

 

Regards,

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...