Previous Table of Contents Next


2.10.1. defvar and defconst

Commonly, you specify the initial value of a global variable with the special form defvar, as in this example from the source for ada-mode.el:

   (defvar ada-language-version ‘ada95
     “*Do we program in ‘ada83’ or ‘ada95’?”)

In this case, ada-language-version is set to ada95, and the variable has documentation written about it that you can access using C-h v (describe- variable).

Moreover, as illustrated in this example, the asterisk at the beginning of the documentation string indicates that the variable is a user option, which means that you can easily set the variable using the commands set-variable, edit-options, or customize.

Interestingly, and confusingly if you are not prepared, defvar does not set a value if the variable already has a value; it only sets a value if the variable is not set, (that is, it is void). This way, you can set a variable yourself ahead of time, before loading the library8 that uses defvar. Otherwise, you could have your settings changed under you by what you load.


8A library is a collection of related function definitions and other expressions, often contained in one file, but sometimes in several.

The nearly similar form defconst unconditionally initializes a variable. defconst changes your setting under you. If you want to change the value of a defconst on startup, first load the library containing it before you set your new value.

As a practical matter, defconst is for variables that are not normally intended to be changed, that is, for constants. You can change a value set with defconst, but normally you would not. Here is an example of a value you would not want to change:

   (defconst pi 3.14159 “Pi to six places.”)

On the other hand, also as a practical matter, the source for c-mode.el makes it convenient for users to change the indentation because so many people have strongly held yet different notions of what the constant should be:

   (defconst c-indent-level 2
    “*Indentation of C statements with respect to containing block.”)

defvar and defconst themselves serve three purposes. They inform you and other people who read the code that the programmer intends to use certain symbols as variables or constants. They tell this to the Lisp system, while providing default values and documentation. Finally, they provide information to utilities such as etags and make-docfile, which create databases of the functions, variables, and constants in a program.

2.10.2. setq

Within a function you generally set a variable using setq, unless you are using a let form or passing the value to the arguments of a definition, both of which create local variables. Variables set with setq are global, unless they are local or buffer local.

Here is an example:

   (setq counter 0)              → 0

This sets the value cell of the symbol counter to 0. The following sets the value cell of the symbol counter to 1 plus whatever the value of counter was earlier; in this case, the earlier value was 0:

   (setq counter (+ 1 counter))   → 1

The following sets the value cell of the symbol counter to 1 plus the previous value; in this case, the previous value was 1:

   (setq counter (+ 1 counter))   → 2

In more detail, here is what happens when you evaluate:

   (setq counter (+ 1 counter))

The Lisp interpreter first evaluates the innermost list; this is the addition. To evaluate this list, it must evaluate the variable counter and the number 1. When it evaluates the variable counter, it receives its current value. It passes this value and the number 1 to the +, which adds them together. The sum is then returned as the value of the inner list and passed to setq, which sets the variable counter to this new value.

2.10.3. Passing an Argument

When you pass an argument to a definition, you create a local variable.

Thus, when you define a function:

   (defun add-37 (number)
     “Add 37 to NUMBER.”
     (interactive “p”)
     (message “The new number is: %d!” (+ 37 number)))

number becomes a local variable, local to the function. You can set the value of number to some other value outside the function, using defvar, defconst, or setq, but inside the function (and inside any functions called by the function, none in this example), the local value of number will shadow the global value.

Here is an example in which number is set to 3, but the local value used by the preceding function definition is 5:

   (setq number 3)          ; Global value of NUMBER is 3

   (add-37 5)               ; Local  value of NUMBER is 5
   “The new number is: 42!”


Previous Table of Contents Next