It's all well that we can call functions, but how the hell do we define some of our own? In this section we will address this question. As always, a simple example will convey the essence quite readily. Consider the following lisp expression:
(defun foo () (message "Hello World\n") (message "Hello world once again!\n"))
Note: message is a built-in function in Elisp that prints a string in the Emacs mini-buffer area (the bottom-most line in an Emacs window; this area is used to accept input for emacs commands and as an echo area to report command output and errors).
A function is a sequence of lisp expressions clubbed together, and typically referred to with a name. In the above example, foo is the name of a function that contains two expressions. The declaration starts from the bracket to the immediate left of the defun keyword to the corresponding close bracket. You must note, and rest reassured, that such a declaration itself conforms to the format of a type 3 expression. There is nothing particularly magical about the syntax of a function declaration in lisp, only the text is formatted a little weird, and you can say the positional arguments have some specific significance. So, if you want to try your hand at declaring a function, type out the above example, and do the C-x C-e routine as explained earlier.
Let us look at another example:
(defun factorial (n)
(if (<= n 0)
1
(* n (factorial (- n 1)))))
This is an implementation of the factorial algorithm. Note that the function body contains only one expression (the if expression.) which is split across multiple lines. The (n) in the first line of the declaration specifies the formal parameter list for this function. In this case, there is only one; if a function needs to take multiple parameters, the names must be specified inside the brackets and separated by white space. One also notes that the types of parameters are not declared anywhere. In fact, this is a feature of lisp in general. Lisp has ``latent types'', which contrasts with ``manifest types'' of other procedural languages; one might also say that Lisp is a dynamically typed language where the type of a variable is determined only at the time of its evaluation. This dynamic nature of the type system means ``generic programming'' comes for free in Lisp. Just think of those poor sods struggling with templates... sigh.
Note that there is no such a thing as a ``return'' statement or expression in lisp. The value of the last evaluated expression in the function is the return value of the function. So, in the above example, the return value of foo is whatever the second message call returns (BTW, message just returns the string it printed out).
The key thing that differentiates a function declaration from other expressions, of course, is the word defun. When the expression declaring a function is evaluated, that function name is registered with the lisp interpreter as a valid function with a body. If you want to change the definition of a function, just write a new defun and eval it. That's it. The old definition will be replaced with your new version. This is one way to 'customise' your Emacs session - by overriding internal Emacs functions with your own; although I must point out that it is not the best of the available options :-)