Constants

In LISP, constants are variables that never change their values during program execution. Constants are declared using the defconstant construct.

Example

The following example shows declaring a global constant PI and later using this value inside a function named area-circle that calculates the area of a circle.

The defun construct is used for defining a function, we will look into it in the Functions chapter.

Create a new source code file named main.lisp and type the following code in it.

(defconstant PI 3.141592)
(defun area-circle(rad)
   (terpri)
   (format t "Radius: ~5f" rad)
   (format t "~%Area: ~10f" (* PI rad rad)))
(area-circle 10)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.

Radius:  10.0
Area:   314.1592

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *