Open In App

Functions in LISP

Improve
Improve
Like Article
Like
Save
Share
Report

A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs.

Defining Functions in LISP: 

Functions in LISP are defined using the DEFUN macro. The basic syntax looks like this : 

(defun function-name (parameters)
  "Documentation string"
  body-of-function
)
  • Name of Function: You can use any kind of symbol as a function name, but usually function names includes only characters and hyphens. In LISP instead of using underscore in naming, hyphens are considered as naming conventions. For example, we write calc-average instead of calc_average or calcAverage.
  • Parameters: These are the variables that are used to retain the arguments that are passed to the function when it is called. These parameters are optional, if a function doesn’t have any parameters, then that list is empty written as ( ) 
  • Documentation String: It is a string literal that is used to describes what a particular function is intended to do.
  • Body of function: The function body consists of LISP expressions which are evaluated in order when the function is called. Usually, the value of the last expression is returned as the value of the function, but through the help of RETURN-FORM which is a special operator, we can return the value of the function from anywhere in it.

Example:

1. Let’s create a function named hello-world that doesn’t take any parameters and returns a hello world string.

Lisp




(defun hello-world () 
  (format t "Hello, World!")
)
(hello-world)


Output:

Hello, World!

2. Function to add two integers

Here we have created a function named add-two-number which takes two arguments n1 and n2. A Documentation string is used to describe the work done by this function and whatever the value is return by calling the + eventually becomes the return value of add-two-number.

Lisp




(defun add-two-number (n1 n2)
  "Adds two numbers"
  (+ n1 n2)
)
(write(add-two-number 10 20))


Output:

30

3. Now let’s create a simple function that takes an optional string argument and return the Welcome string : 

In the function welcome-to-gfg,  we have one optional parameter name. To define any optional parameter add the symbol: &optional before the name of the parameter which is optional.
We have set its default value to Sandeep, So if the user calls the welcome-to-gfg function without any arguments it prints “Welcome “Sandeep” to Geeks for geeks!” or if any argument is passed while calling the function it will return the string which contains the argument value passed while calling the function.
(terpri) is used to print a new line

Lisp




(defun welcome-to-gfg (&optional (name "Sandeep"))
  (format t "Welcome ~S to Geeks for geeks!" name)
)
(welcome-to-gfg)
(terpri)
(welcome-to-gfg "UserName")


Output: 

Welcome "Sandeep" to Geeks for geeks!
Welcome "UserName" to Geeks for geeks!


Last Updated : 06 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads