w In Lisp programs are made up of interrelated functions.
w A function is a short piece of code to accomplish a specific task.
w The basic idea of a function in mathematics is that a function has zero or
more input values and one or more output values. The actual output
value(s) produced depend directly on the input values. We say that the
output is a function of the input.
w In mathematics the function name is followed by a left parenthesis, followed by
the input values separated by commas, followed by a right parenthesis. Thus,
mult(2,4) would be the equivalent of 2 x 4.
w In Lisp the right parenthesis comes first and there are no commas to separate the
input values. Thus: (mult 2 4).
Historical Note: This way of designating a function and its arguments is a form of
prefix notation, a variation of Polish notation (so named in honor of Polish
logician Jan Lukasiewicz), and is called Cambridge Polish notation.
w When defining a function in a programming language, we refer to the arguments
with which the function will be called as formal parameters, formal arguments,
or sometimes simply parameters.
w In Lisp, a user-defined function is specified with the built-in macro, defun.
w The general format is:
(defun <function-name> (<parameter-list>) <function-body>)
w Notice that the parameters are enclosed within parentheses. If there are no
parameters, that is indicated with a set of empty parentheses.
w <function-body> is Lisp code which specifies how to calculate the output
value(s) from the input value(s).
w Example 1:
(defun mult1 (multiplicand multiplier)
(* multiplicand multiplier))
w Example 2:
(defun mult2 (x y) (* x y))
w Functions can be defined in Lisp by simply typing a function definition while in
Lisp's interactive mode. But these definitions are lost as soon as one exits from
w A better way to define functions is to place function definitions into a file using
an editor, and then load them into Lisp.
1. Try out both Examples 1 and 2. First define each function. Then try
out various input values to see if they give the correct answer.
2. Define and test add3num, which takes 3 numbers as input values and sums
3. Define and test prodtriple, which takes 2 numbers, x and y, and returns
the value: 3xy.