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!
Similar Reads
Lambda Functions in LISP In this article, we will discuss lambda functions in LISP. The Lambda function is used to evaluate a mathematical expression in our program. They are also known as anonymous functions. We can create these functions using lambda expression. Syntax: (lambda (parameters) expression_code) where, The par
1 min read
Output Functions in LISP Output functions need an optional argument known as output stream which by default is "output-stream" and can be assigned to another stream where the output has to be sent. Write:write object  key : stream Example 1: Lisp ; LISP program for Write (write "Hello World") (write "To") (write "The") (wr
3 min read
Mapping Functions in LISP In this article, we will discuss mapping functions in lisp. Mapping functions are applied on the list data structure for combining one or more lists of elements. By using this we can perform mathematical operations and can join the elements. The main advantage of this function is that we can combine
2 min read
Atoms in LISP Pre-requisites: Introduction to LISP Function Languages are those languages in which the basic building block is functions. In functional programming, the programmer is concerned only with functionality and not memory related variable storage and assignment sequence. There are some commonly used Fun
2 min read
parse() Function in R The parse() function in R programming language is used to return the parsed but unevaluated expression of a given expression in an expression, a âlistâ of calls. Also, this function converts an R object of the character class to an R object of the expression class. Syntax: parse(file = "", Â n = NULL
2 min read
Functions in Programming Functions in programming are modular units of code designed to perform specific tasks. They encapsulate a set of instructions, allowing for code reuse and organization. In this article, we will discuss about basics of function, its importance different types of functions, etc.Functions in Programmin
14 min read