Results 1 to 7 of 7

Thread: DEFUN's "arguments"

  1. #1
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default DEFUN's "arguments"

    Hi ALL,

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

    Code:
    (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.

  2. #2
    Active Member
    Join Date
    2008-06
    Location
    Australia
    Posts
    51
    Login to Give a bone
    0

    Default Re: DEFUN's "arguments"

    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.

  3. #3
    Certifiable AUGI Addict tedg's Avatar
    Join Date
    2005-06
    Location
    in the upper right corner
    Posts
    3,505
    Login to Give a bone
    0

    Default Re: DEFUN's "arguments"

    Quote Originally Posted by BoKirra View Post
    Hi ALL,

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

    Code:
    (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 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.

  4. #4
    Administrator rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Earth
    Posts
    9,803
    Login to Give a bone
    0

    Default Re: DEFUN's "arguments"

    Quote Originally Posted by BoKirra View Post
    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.

    Code:
    
    (defun foo (x)
      (* 1.46666666 x)
    )
    
    (foo 60)
    ;; returns 88
    
    (foo 75)
    ;; returns 110
    
    R.K. McSwain | CAD Panacea |

  5. #5
    I could stop if I wanted to
    Join Date
    2005-09
    Location
    Canada
    Posts
    214
    Login to Give a bone
    0

    Default Re: DEFUN's "arguments"

    you can use any type of arguments.

    eg:

    Code:
    (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:


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

  6. #6
    Login to Give a bone
    0

    Post Re: DEFUN's "arguments"

    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:
    Code:
    (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:
    Code:
    (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/Tut...#IntroAutoLISP

  7. #7
    AUGI Addict
    Join Date
    2006-04
    Location
    (getpoint "Anywhere on the Enter Key =>")
    Posts
    1,160
    Login to Give a bone
    0

    Default Re: DEFUN's "arguments"

    Quote Originally Posted by Terry Cadd View Post
    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:
    Code:
    (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:
    Code:
    (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/Tut...#IntroAutoLISP
    Thanks for your help, Terry.
    And thanks to ALL.
    Now what I need is - more practices.

Similar Threads

  1. Nesting Defun with Arguments and Variables
    By Jeff@BOC in forum AutoLISP
    Replies: 10
    Last Post: 2014-04-16, 12:46 AM
  2. Replies: 7
    Last Post: 2012-12-11, 01:41 PM
  3. Replies: 0
    Last Post: 2012-06-06, 11:54 AM
  4. ENTIDADES EN ALIGNMENT COMO "FIXED", "FLOTING" y "FREE"
    By cadia in forum AutoCAD Civil 3D - General
    Replies: 1
    Last Post: 2009-02-01, 04:21 AM
  5. Replies: 1
    Last Post: 2006-06-13, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •