0

I am trying to call the following function:

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)

Inside this LOOPER function:

(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (func) ;this is obviously the problem
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)   

Like this:

(defun c:Adder ()
    (LOOPER (c:Add))
)

How do I get around the fact that func is undefined in LOOPER function?

1
  • 1
    Wouldn't (LOOPER (c:Add)) call LOOPER with the resulting number from calling (c:Add) once? It's not the same as passing the function c:Add as a parameter. Commented Jun 24, 2018 at 1:12

2 Answers 2

2

You can pass a function as an argument to another function as demonstrated by the following example:

(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        (func)
    )
)

(defun c:adder ( )
    (looper c:add)
)

Here, the symbol c:add is evaluated to yield the pointer to the function definition, which is then bound to the symbol func within the scope of the looper function. As such, within the scope of the looper function, the symbols func and c:add evaluate the same function.

Alternatively, you can pass the symbol c:add as a quoted symbol, in which case, the value of the symbol func is the symbol c:add which may then be evaluated to yield the function:

(defun c:add ( / a )
    (if (setq a (getint "\nEnter a number to add 2 to it: "))
        (+ a 2)
    )
)

(defun looper ( func )
    (while
        (progn
            (initget "Y N")
            (/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
        )
        ((eval func))
    )
)

(defun c:adder ( )
    (looper 'c:add)
)

Passing a quoted symbol as a functional argument is more consistent with standard AutoLISP functions, such as mapcar, apply etc.

Sign up to request clarification or add additional context in comments.

1 Comment

@lee mac great I will implement this technic.
0

As far as I know, you can not send function name as the parameter, but here I give you a technic which can act like similar.

I don't have Autocad install on my machine so I am unable to test this code. but you can remove if there is any small mistake or grab the concept so you can implement your own.

(defun c:Add ()
    (setq a (getint "Enter a number to add 2 to it"))
    (setq a (+ a 2))
)

(defun c:sub ()
    (setq a (getint "Enter a number to substract from 2:"))
    (setq a (-2 a))
)

(defun c:mul ()
    (setq a (getint "Enter a number to multiply with 2:"))
    (setq a (* a 2))
)


;----This function use to call other function from function name
;----Function name string is case sensitive
;----As per need you can Add function name to this function
(Defun callFunction(name)
(setq output nil)
;here you can add nested if condition but for simplicity I use If alone
(if (= name "C:Add")(setq output (C:Add)))
(if (= name "C:sub")(setq output (C:sub)))
(if (= name "C:mul")(setq output (C:mub)))
output
)

;----------Function end here



(defun LOOPER (func)
    ;repeats 'func' until user enters 'no'
    (setq dummy "w")
    (while dummy
        (callFunction func) ;Change here
        (setq order (getstring "\nContinue? (Y or N):"))
        (if (or (= order "N") (= order "n")) (setq dummy nil))
    )
)

You like run this program like this:

(defun c:Adder ()
    (LOOPER ("c:Add"))
)

(defun c:substaker ()
    (LOOPER ("c:sub"))
)

(defun c:multiplyer ()
    (LOOPER ("c:mul"))
)

Hope this helps:

1 Comment

You're a legend mate. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.