Unless told otherwise, Lisp expects that a function with an argument in its function definition will be called with a value for that argument. If that does not happen, you get an error and a message that says `Wrong number of arguments'.
However, optional arguments are a feature of Lisp: a keyword may
be used to tell the Lisp interpreter that an argument is optional.
The keyword is
&optional. (The `&' in front of
`optional' is part of the keyword.) In a function definition, if
an argument follows the keyword
&optional, a value does not
need to be passed to that argument when the function is called.
The first line of the function definition of
therefore looks like this:
(defun beginning-of-buffer (&optional arg)
In outline, the whole function looks like this:
(defun beginning-of-buffer (&optional arg) "documentation..." (interactive "P") (push-mark) (goto-char (if-there-is-an-argument figure-out-where-to-go else-go-to (point-min))))
The function is similar to
interactive expression has
"P" as an argument and
goto-char function is followed by an if-then-else expression
that figures out where to put the cursor if there is an argument.
"P" in the
interactive expression tells Emacs to pass
a prefix argument, if there is one, to the function. A prefix argument
is made by typing the META key followed by a number, or by typing
C-u and then a number (if you don't type a number, C-u
defaults to 4).
The true-or-false-test of the
if expression is simple: it is
simply the argument
arg has a value that is not
nil, which will be the case if
called with an argument, then this true-or-false-test will return true
and the then-part of the
if expression will be evaluated. On the
other hand, if
beginning-of-buffer is not called with an
argument, the value of
arg will be
nil and the else-part
if expression will be evaluated. The else-part is simply
point-min, and when this is the outcome, the whole
goto-char expression is
(goto-char (point-min)), which is
how we saw the
beginning-of-buffer function in its simplified
Go to the first, previous, next, last section, table of contents.