Elisp: Function Parameters (optional, rest)

By Xah Lee. Date: . Last updated: .

Optional Parameters

To have optional parameters, add &optional at the end of parameter list. Any parameter after that is optional.

;; defining a function with 2 optional params named cc and dd

(defun ff (aa bb &optional cc dd)
  "test optional params"
  (interactive)
  (message "%s %s %s %s" aa bb cc dd))

;; call it
(ff 1 2)
;; "1 2 nil nil"

(ff 1 2 3)
;; "1 2 3 nil"

(ff 1 2 3 4)
;; "1 2 3 4"

Rest Parameters (Unspecified Number of Parameters)

To specify unspecified number of parameters, add &rest name after the last parameter. In the function, value of name is a list, or nil if not given.

You can have both &optional and &rest, in that order.

Argument List (ELISP Manual)

;; defining a function with rest parameters

(defun ff (aa bb &rest cc)
  "test rest arguments"
  (message "%s" cc) ; cc is a list
  )

;; test
(ff "1" "2" "3" "4")
;; ("3" "4")

No Default Values

Function Keyword Parameters (Named Parameters)

Elisp, Function