+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Forum Newbie
    Using
    Architecture 2010
    Join Date
    Jun 2009
    Posts
    3

    Default Passing variables between functions

    Registered forum members do not see this ad.

    Gidday,

    Basically what I am looking to do is run one Autolisp function that will define a global variable and then have another function use that variable. Both functions need to be able to be run from the command line or from a button on the ribbon at any time (the second will be error trapped so, if the first hasn't been run and the variable hasn't been defined, it will return a sensible error).

    What I am trying is something like this:

    Code:
    (defun c:CreateVariable ( / localVariable1 localVariable2)
      (setq localVariable1 50.0)
      (setq localVariable2 3.0)
      (setq globalVariable (* localvariable1 localVariable2)
    ) ; end defun
    
    (defun c:UseVariable ()
      (setq newVariable (/ 1234.0 globalVariable))
      (princ (rtos newVariable))
    ) ; end defun
    I know that if you define a function using c: xxxx it wont let you use arguments but I don't know how to get that global variable into the function...

    I'm sure it's simple, but at the moment it's very frustrating.

    Any help would be appreciated.

  2. #2
    Senior Member
    Computer Details
    jammie's Computer Details
    Operating System:
    W7
    Discipline
    Civil
    jammie's Discipline Details
    Occupation
    Project Technician
    Discipline
    Civil
    Using
    AutoCAD 2016
    Join Date
    May 2006
    Location
    Ireland
    Posts
    316

    Default

    Registered forum members do not see this ad.

    Introduce a test that will run the CreateVariable if the globalVariable has not been defined

    Code:
     (if
      (not globalVariable) ;test expression
      (c:CreateVariable) ;preform the function
      )
    See the test expressions COND, IF, AND OR in the AutoLISP help file for more info

    Code:
    (defun c:CreateVariable ( / localVariable1 localVariable2)
      (setq localVariable1 50.0)
      (setq localVariable2 3.0)
      (setq globalVariable (* localvariable1 localVariable2))
    ) ; end defun
    
    (defun c:UseVariable ()
       
      ;Either the variable has a value or assign one
      (or globalVariable (c:CreateVariable))
    
      (setq newVariable (/ 1234.0 globalVariable))
      (princ (rtos newVariable))
    ) ; end defun

Similar Threads

  1. Passing variables from VBA to Lisp
    By TomJandro in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 14th Aug 2009, 12:49 pm
  2. passing value from command line to lisp function
    By shamsam1 in forum AutoLISP, Visual LISP & DCL
    Replies: 10
    Last Post: 4th Aug 2008, 03:02 pm
  3. sample code for passing parameters to another function
    By Nestor in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 26th Jun 2008, 02:02 am
  4. Passing data to table field
    By Impala62 in forum AutoLISP, Visual LISP & DCL
    Replies: 10
    Last Post: 13th Mar 2008, 05:06 pm
  5. some help with functions please
    By Rebbeca in forum AutoLISP, Visual LISP & DCL
    Replies: 10
    Last Post: 22nd Nov 2006, 03:18 pm

Bookmarks

Posting Permissions

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