next up previous
Next: Global and 'Local' Variables Up: Variable Operations Previous: Variable Operations

Defining Variables

As we mentioned earlier, we do not really need to either declare or define a variable formally to use it. We can just setq a variable and it will be brought into existence if it is not already so. However, there are a couple of special forms defvar and defconst that can be used to define a variable, if you feel like it. A proper definition serves two important purposes. First, they inform people who read the code that certain symbols are intended to be used a variables. Second, special forms allow one to specify a documentation string, which Emacs can then make available through the Help infrastructure. Neither of these is not possible setq.

defvar has an interesting property that is worth noting here. Suppose a variable is already set to any value (i.e. it 'exists') then defvar will not alter the value. It will just take the doc. string and move on. This is extremely useful when changing default settings in your  /.emacs. Let's say you want to change the default value (setq calendar-setup 'two-frames) You do not need to worry if calendar.el (which has the definition for calendar-setup) was loaded in before your that line in your .emacs is executed. Either way, the value of your calendar-setup will be set to the value you choose, and not the default defined in calendar.el

The purpose of defconst should be fairly clear from its name. Before using it, though, you should look up its documentation, for there are some things you should be aware that we are not going into here.


next up previous
Next: Global and 'Local' Variables Up: Variable Operations Previous: Variable Operations
Sriram Karra
2005-01-06