Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are arguments passed into functions in Elisp?

Coming from a C++ background, I'm trying to figure out how arguments are passed into methods in Elisp. While I acknowledge that maybe the wording could be different, I'm wondering if it is closer to the C++ idea of passing by reference or passing by value? If I alter the parameter in the method itself, will it alter the parameter that was passed in in the function call?

like image 213
joe_04_04 Avatar asked Jan 29 '23 20:01

joe_04_04


1 Answers

All Lisps (Emacs Lisp, Common Lisp) pass parameters by value, always:

(defparameter x 42)  ; defconst in Emacs Lisp
(defun test (x)
  (setq x 10))
(test x)
==> 10
x
==> 42

Note, however, that some values are actually pointers (or, rather, objects with components), so a function can modify their content by side effects:

(defparameter x (list 1 2))
(defun test (x)
  (setf (first x) 42
        (second x) 24
        x 17))
(test x)
==> 17
x
==> (42 24)

PS1. Cf. When to use ' (or quote) in Lisp? -- "quoted arguments" are evaluated too: the evaluation strips the quote.

PS2. Cf. add-to-list - it accepts a symbol (variable name) and modifies its value. This only works for global dynamic variables, not for lexical variables. Not a very good idea.

like image 116
sds Avatar answered Feb 04 '23 09:02

sds