Adam Lee
Adam Lee

Reputation: 576

Unable to use function passed in as parameter?

I am trying to use a function (secondaryFunction or secondaryFunction2) passed in as a parameter to primaryFunction. However, when I run my code below:

(defun secondaryFunction (param1 param2)
    NIL
)

(defun secondaryFunction2 (param1 param2))
    NIL
)

(defun primaryFunction (transition param1 param2)
    (transition param1 param2)
)


(primaryFunction 'secondaryFunction 0 0)

I get the following error:

*** - EVAL: undefined function TRANSITION

This seems strange, considering that I thought that I passed in secondaryFunction clearly as the transition parameter to primaryFunction?

Upvotes: 0

Views: 84

Answers (1)

Svante
Svante

Reputation: 51501

There are separate function and value namespaces. In primary-function, you get the transition function as a value (actually the symbol here). Use funcall to call it:

(defun primary-function (transition param1 param2)
  (funcall transition param1 param2))

Side Note: You are using a symbol, which means that funcall calls the function with that symbol as a name in the global environment. That's OK. You can also pass the function itself, with the function special operator (which has a shorthand #' reader macro):

(primary-function #'secondary-function 0 0)

Upvotes: 3

Related Questions