+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Super Member
    Using
    AutoCAD 2010
    Join Date
    Dec 2010
    Posts
    1,246

    Default defun inside another defun

    Registered forum members do not see this ad.

    Hi

    i saw some programmers use (defun) inside (defun),
    what is the use of it?

    i were thinking using it when interacting with user like so :

    Code:
    (defun C:de1()
      (setq f t)
      (while f
        (initget 7 "Shay Dan")
        (setq s (getkword "enter a name[Shay/Dan]"))
    
        (cond
          ((eq s "Shay") (shay))
          ((eq s "Dan") (dan))
          (T s)
          )
    
          )
       
    
      (defun shay()
        (princ "enter shay")
        (setq f nil)
        )
    
      (defun dan()
        (princ "enter dan")
        )
    )
    but i get error : no function definition

    Thanks
    Shay

  2. #2
    Senior Member
    Computer Details
    Snownut's Computer Details
    Operating System:
    Windows 10 Home premium
    Computer:
    Lenovo
    Using
    not applicable
    Join Date
    Nov 2010
    Location
    New Hampshire
    Posts
    238

    Default

    That's because once the main function is started it loads all data according to its position in the function, if you moved your defun's ahead of the setq's and other code then all should be good. Typically all internal functions are placed above all calling code.

  3. #3
    Quantum Mechanic BIGAL's Avatar
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    10,800

    Default

    The example above is not really what I would call a defun inside a defun I have global defuns that get autoloaded so when inside another lisp you do not need the code to be continually repeated. A very simple example could be a global make line defun (linemake pt1 pt2). The sort of thing I used defun inside defun was for a large suite of intergrated lisps so that objects or steps or interface was always consistent across any program in the suite. Often I see better ways of doing things so a global defun means its straight away available for an existing variety of lisps.

    A better example is using a pline defun to provide all the details about the pline even if it gives extra stuff thats not used. Just copy and paste or call it globally. A 100 plines a 100 calls to the defun.
    A man who never made mistakes never made anything

  4. #4
    Senior Member cwake's Avatar
    Discipline
    Civil
    cwake's Discipline Details
    Occupation
    Civil Designer
    Discipline
    Civil
    Using
    AutoCAD 2015
    Join Date
    Jun 2011
    Location
    Queensland Australia
    Posts
    133

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by samifox View Post
    what is the use of it?
    It allows you to create a lisp routine that has no side effects on your drawing namespace.
    If you localise names of sub-functions (like you would a local variable) then once the lisp has completed the symbol names that you used will revert to whatever value they were prior to running the lisp. Most of the time that will be 'nil, but not always. And that becomes very important if you are operating in a customised corporate environment. It is common with corporate customisations to define global functions that are available (like a library) to be called from other functions. I think BIGAL is referring to this practice. But countless times I have seen secondary lisps (not part of the corporate customisation) that have the potential to redefine corporate functions with unpredictable side-effects.
    I think that the best practice is to define global functions only if they form part of a customisation, but when doing so, use a naming convention that is unlikely to be used by others. eg. use the company name as a prefix to all global functions.
    For lisp routines that are not part of a corporate customisation, keep the side-effects of the routine to a minimum. Localise everything you can.
    Clint

Similar Threads

  1. (defun c: LISP THAT I NEED)
    By Qonfire in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 18th Jul 2011, 04:09 am
  2. [Help] What wrong in Fix defun ?
    By ketxu in forum AutoLISP, Visual LISP & DCL
    Replies: 8
    Last Post: 14th Jun 2011, 01:12 am
  3. Defun - no fun!
    By firavolla in forum AutoLISP, Visual LISP & DCL
    Replies: 16
    Last Post: 18th Jun 2010, 12:24 pm
  4. Defun AT:?????
    By chulse in forum AutoLISP, Visual LISP & DCL
    Replies: 17
    Last Post: 14th Jun 2010, 08:20 am
  5. (defun cb:...)
    By bartdewilde in forum AutoLISP, Visual LISP & DCL
    Replies: 8
    Last Post: 17th Jun 2009, 12:11 am

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