Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Keyword Parameters in LISP

  • Last Updated : 29 Oct, 2021

LISP Function‘s parameters list has a basic aim of declaring the variables which will receive the arguments that are passed in the function. Normally parameter list contains a basic list of variable names, those parameters are called “required parameters”. If a function is called, for every required parameter it should be supplied with one individual argument, if it’s called with more or fewer arguments, LISP will show an error.

Types of parameters in the parameters list

  • Required parameters
  • Optional Parameters
  • Rest Parameters
  • Keyword Parameters

Keyword Parameters:

Keyword parameters are used when we want to pass a specific value to a specific parameter. Consider a scenario where there are 3 optional parameters, what if the user wants to pass just a second optional parameter’s value, here the first parameters even though is optional it will be mandatory for him to pass its value making it a required parameter, to solve this problem keyword parameters are used where the caller has a choice to specify which value will be specified to which particular parameter.

The syntax for defining Keyword Parameters:

To define keyword parameters in a function, add the symbol &key before any parameters that are supposed to be set as keyword parameters.
To illustrate this look at the code below: 

(defun keyword_example (&key x y z)...)

Here x y z are keyword parameter specifiers.

Example:

Now let’s create a function that just has keyword parameters and also set their default values to some predefined values.

(defun keyword_example (&key (x 5) (y 7) (z (+ x y)))...)

In this function called “keyword_example” we have 3 keyword parameters, 
In addition to that, we have set default values for all those parameters as for x -> 5, for y -> 7, and for z we are giving it the value of the sum of x and y

Now, for illustration purposes, in the body of this function, we will create a list that will contain our parameters

(list x y z)

Let’s call this function by having various calls with different values for each parameter

Code : 

Lisp




(defun keyword_example (&key (x 5) (y 7) (z (+ x y)))
  (list x y z))
  
(write(keyword_example 😡 1))      
(terpri)    
(write(keyword_example :y 1) )         
(terpri)    
(write(keyword_example :y 1 :z 4) )    
(terpri)    
(write(keyword_example :y 1 :z 4 😡 2 ))

Output: 

(1 7 8)
(5 1 6)
(5 1 4)
(2 1 4)

Here, at the first call, we just passed the value for x by specifying its value with 😡 symbol, for the later function calls we set values for different parameters specifically, Also there is no need to pass the arguments in a specific order as we are using keyword specifiers(see 4th function call) thus with keyword parameters, we were able to give a specific parameter it’s a specific value.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!