Author Topic: Whats difference between ...?  (Read 6094 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Whats difference between ...?
« on: July 08, 2010, 06:46:45 AM »
Whats difference between
Code: [Select]
(defun c:test [color=red]()[/color]
  )
.
Code: [Select]
(defun c:test[color=red](/)[/color]
  )
,
Code: [Select]
(defun c:test [color=red](/ var1 var2)[/color]
  )
and
Code: [Select]
(defun c:test [color=red](var1 / var2)[/color]
  )


Up to my little knowladge
First case will not reset the variables value when code finished.
Third case will reset the variables value when code finished.


Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Whats difference between ...?
« Reply #1 on: July 08, 2010, 07:27:47 AM »
Good Question
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #2 on: July 08, 2010, 07:45:58 AM »
1 and 2 are the same

3 you are localizing 2 variables  var1 and var2

4 you are passing and argument  var1  (it really should read arg1)  and localizing a variable var2
-essentially you are giving the routine some information to work with

HTH
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Whats difference between ...?
« Reply #3 on: July 08, 2010, 08:10:03 AM »
Thanks.  But this leads to another question. 

IN layman terms, What is an argument?  And when and why should I use them?
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Whats difference between ...?
« Reply #4 on: July 08, 2010, 08:12:20 AM »
Quick Example of Argument passing/localising variables:

Code: [Select]
(defun c:test ( / num )

  (if (setq num (getint "\nPick a Number: "))
    (doit num)
  )
  (princ)
)

(defun doit ( n / str )
  (setq str (strcat "You chose the number: " (itoa n)))
  (alert str)
)

Notice how the variable 'num' is passed as an argument to the function 'doit'. The data bound to the symbol 'num' is then bound to the symbol 'n' and can be manipulated by the 'doit' function.

Notice also that the variables 'str' and 'num' are localised in their respective functions.

Lee

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #5 on: July 08, 2010, 08:14:20 AM »
You would use an argument when you want to give the sub some information to process;

Code: [Select]
(defun add ( n1 /)

(+ 10 n1)
)

In that example you would pass a integer to the sub to be added to 10

(add 30)

should return 40

There were no variables needed in that example.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Whats difference between ...?
« Reply #6 on: July 08, 2010, 08:44:04 AM »
I am still foggy on the issue.  I (in my limited lisp writing knowledge) would write it this way:
Code: [Select]
(defun c:test (/ num str)
  (setq num (getint "\nPick a Number: "))
  (setq str (strcat "You chose the number: " (itoa num)))
  (alert str)
  (princ)
)

Thus no need for an agument.  I know lisp writing can be done many different ways but I am trying to see the benefit because there has to be at least one if you guys are using them.

And how did sub get the information?  I do not see the variable num in the sub thus making the link.


I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Whats difference between ...?
« Reply #7 on: July 08, 2010, 08:49:53 AM »
The 'link' is in passing the data to the subfunction via the argument - the symbol 'n' in my example is the argument and the data is bound to it when the subfunction is called.

As for why, perhaps consider how you would rewrite this example:

Code: [Select]
(defun c:test nil

  (mapcar 'dtr (list 0.0 45.0 90.0 135.0 180.0))

)

(defun dtr ( a )
  (* pi (/ a 180.0))
)

 :-)

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #8 on: July 08, 2010, 08:58:10 AM »
Lets look at the degrees to radians function

Code: [Select]
(defun DTR (numberOfDegrees)
 (* pi (/ numberOfDegrees 180.0))
)

When you need to convert degrees to radians in one of your functions you make a call to this function.  You would need to pass that function an argument (in this case degrees).  In turn this function would return the number of radians in the supplied degrees.  So it gets the info from the programmer of from another function in your program.

This is used when you have to do a lot of converting.  It will cut down the amount of code needed by creating a sub to handle the conversion rather than typing this everytime you need to convert:

Code: [Select]

(setq rads (* pi (/ "yourdegreeshere" 180.0)))

it would be like this instead
Code: [Select]
(setq rads (DTR "yourdegreeshere"))

I an horrible at explaining this sort of thing.  Sorry. :|
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #9 on: July 08, 2010, 08:58:49 AM »
That is funny that we used the same code as an example........ :lol:
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Whats difference between ...?
« Reply #10 on: July 08, 2010, 08:59:52 AM »
That is funny that we used the same code as an example........ :lol:

I was just thinking the same thing...  :lol:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Whats difference between ...?
« Reply #11 on: July 08, 2010, 09:10:07 AM »
Perhaps also look at the help on 'defun' and:

Using the AutoLISP Language > AutoLISP Basics > Symbol and Function Handling > Functions with Arguments

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Whats difference between ...?
« Reply #12 on: July 08, 2010, 09:36:50 AM »
Sorry Lee I am still not seeing the link in you DTR example.  I can in Tim's and in autocads help - sort-of.  How is "a" getting it data.   Furthermore (and this is embarrassing) I have never needed the DTR and RTD functions.  I have one lisp that uses rads and I left it processing the rads. Degrees were not needed becuase it would be irrelevant processing that the user would never see.  Draw a line at this angle, period. 

Code: [Select]
  (setq ANG1 (angle UP1 UP2))
  (setq ANG2 (angle UP2 UP3))

 I use sub functions but more to grope my code in chunks.  Really not needed but it makes sense to me.



What is even more embarrassing is that I have never needed to process a list.  Unless it was in a fish that was thrown to me.
So asking me to rewrite your example that would be a class in itself.  :oops:
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Whats difference between ...?
« Reply #13 on: July 08, 2010, 09:42:15 AM »
Perhaps also look at the help on 'defun' and:

Using the AutoLISP Language > AutoLISP Basics > Symbol and Function Handling > Functions with Arguments

I will.
Thanks.
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Whats difference between ...?
« Reply #14 on: July 08, 2010, 09:44:21 AM »
Ok, perhaps 'mapcar' was a bad example (I'm also not too good at explaining this stuff...) this is perhaps a better example:

Consider the function 'angle', this function takes two arguments - two points, and returns the angle between them.

What we are effectively doing is creating our own 'library functions' taking arguments just as functions such as 'angle' etc do.

In our previous examples, the 'dtr' function takes one argument (a real number) and returns a real number. (I too don't use degrees all too often - don't worry about it).

Code: [Select]
(defun dtr ( a )
  (* pi (/ a 180.0))
)

Here, 'a' represents the argument (real number) that the user needs to provide in order to use the function.

Hence just as we do this:

Code: [Select]
(angle <pt1> <pt2>)
We can do this:

Code: [Select]
(dtr <real>)

« Last Edit: July 08, 2010, 09:47:23 AM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #16 on: July 08, 2010, 10:29:31 AM »
How is "a" getting it data. 

Code: [Select]
  (setq ANG1 (angle UP1 UP2))
  (setq ANG2 (angle UP2 UP3))

 I use sub functions but more to grope my code in chunks.  Really not needed but it makes sense to me.


"a" is gotten from you or the program itself.  Take your example

Code: [Select]
(setq a (angle UP1 UP2));; This sets "a"
 (setq d (RTD a));; This calls RTD and sends "a" as an argument. It converts "a" and sets "d" to the conversion.


Code: [Select]
(defun RTD (Rads /)
 (* 180.0 (/ Rads pi))
)

ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

hermanm

  • Guest
Re: Whats difference between ...?
« Reply #17 on: July 08, 2010, 10:41:48 AM »
1 and 2 are the same

3 you are localizing 2 variables  var1 and var2

4 you are passing and argument  var1  (it really should read arg1)  and localizing a variable var2
-essentially you are giving the routine some information to work with

HTH

Tim, you probably already know this , but for anyone who does not,

The arguments (sometimes called parameters in the function definition) also become local variables (local symbols) to the function.

So, you can do this:
(contrived ridiculous example)

Code: [Select]
Command: (defun foo ( a / ) (setq a (+ 5 a)) (princ (itoa a)))
FOO

Command: (foo 12)
17"17"

Meaning, if you need to process an argument in some way, then do multiple things with the modified value, you need not define a local symbol to hold the modified value, because the argument is itself a local symbol.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Whats difference between ...?
« Reply #18 on: July 08, 2010, 11:03:56 AM »
I think the example was a good one.  :-)

I would have gone with a bit less: 

Code: [Select]
Command: (defun foo ( a / ) (setq a (+ 5 a)))
FOO

Command: (foo 12)
17

I didn't see the need to convert the value.   :-P (just having a slow day, don't mind me)
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Whats difference between ...?
« Reply #19 on: July 08, 2010, 11:38:14 AM »
A more concrete example would be taking a folder path as a string.  If the function is expecting a trailing "\\" at the end, the argument can be checked and modified if necessary without having to track a separate variable.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}