6

I tried to determine if Emacs returned object by reference or value, but could not find a clear answer (searching google and Emacs Lisp manual). I guess it must be "by reference", and I just overlooked it somewhere..

For example:

(defun my-get-hash ()
  (let ((h (make-hash-table :test 'equal)))
    (puthash "a" 1 h)
    h))

(let ((h2 (my-get-hash)))
  (message (prin1-to-string h2)))

Here the hash h is created with a let binding, and then return to the caller. Does the caller receive a reference or a copy of h?

Håkon Hægland
  • 3,648
  • 1
  • 22
  • 51

2 Answers2

7

It's by reference when it comes to complex types like vectors and lists (or, more correct, cons pairs).

Simple types like symbols and numbers are not mutable, so they can be seen as "by value".

Lindydancer
  • 6,150
  • 1
  • 15
  • 26
0

As said in https://stackoverflow.com/questions/48363798/how-are-arguments-passed-into-functions-in-elisp Passed by assignment, new variables created just like assignment works. Symbol of variable is evaluated first and then assignment to new symbol.

https://www.gnu.org/software/emacs/manual/html_node/elisp/Mutability.html

Mutability defines ability of functions to have side effect to argument.

If there is exist a function in Emacs Lisp that may modify type of argument then this argument may be considered as may be affected by side effect.