Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a function as a parameter to in elisp?

Tags:

I'm trying to pass one method to another in elisp, and then have that method execute it. Here is an example:

(defun t1 ()   "t1")  (defun t2 ()   "t1")  (defun call-t (t)   ; how do I execute "t"?   (t))  ; How do I pass in method reference? (call-t 't1) 
like image 848
oneself Avatar asked Oct 17 '08 18:10

oneself


People also ask

How do you pass a function as a parameter to another function?

Function Call When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this: func(print); would call func , passing the print function to it.

Can you pass functions as parameters?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, a function greet is created which takes a function as an argument.

Can we pass a function as a parameter in for function?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.

What is it called when you pass a function as a parameter?

I will call what you are passing in a to a function the actual parameters, and where you receive them, the parameters in the function, the formal parameters.


1 Answers

First, I'm not sure that naming your function t is helping as 't' is used as the truth value in lisp.

That said, the following code works for me:

(defun test-func-1 ()  "test-func-1"    (interactive "*")    (insert-string "testing callers"))  (defun func-caller (callee)   "Execute callee"   (funcall callee))  (func-caller 'test-func-1) 

Please note the use of 'funcall', which triggers the actual function call.

like image 141
Timo Geusch Avatar answered Sep 28 '22 06:09

Timo Geusch