Skip to main content

fboundp

fboundp Function

Syntax:

fboundp name → generalized-boolean

Pronunciation:

[ ef ba_undp—e ]

Arguments and Values:

name—a function name.

generalized-boolean—a generalized boolean.

Description:

Returns true if name is fbound; otherwise, returns false.

Examples:

(fboundp ’car) → true 
(fboundp ’nth-value) → false
(fboundp ’with-open-file) → true
(fboundp ’unwind-protect) → true
(defun my-function (x) x) → MY-FUNCTION
(fboundp ’my-function) → true
(let ((saved-definition (symbol-function ’my-function)))
(unwind-protect (progn (fmakunbound ’my-function)
(fboundp ’my-function))
(setf (symbol-function ’my-function) saved-definition)))
→ false
(fboundp ’my-function) → true
(defmacro my-macro (x) ‘’,x) → MY-MACRO
(fboundp ’my-macro) → true
(fmakunbound ’my-function) → MY-FUNCTION
(fboundp ’my-function) → false
(flet ((my-function (x) x))
(fboundp ’my-function)) → false

Exceptional Situations:

Should signal an error of type type-error if name is not a function name.

See Also:

symbol-function, fmakunbound, fdefinition

Notes:

It is permissible to call symbol-function on any symbol that is fbound.

fboundp is sometimes used to “guard” an access to the function cell, as in: (if (fboundp x) (symbol-function x))

Defining a setf expander F does not cause the setf function (setf F) to become defined.

Expanded Reference: fboundp

Testing Built-in and User-defined Functions

fboundp returns true if the given symbol has a function binding in the global environment. It works for functions, macros, and special operators.

;; Built-in functions are fbound
(not (null (fboundp 'car)))
=> T
(not (null (fboundp '+)))
=> T

;; User-defined functions
(defun greet (name) (format nil "Hello, ~A!" name))
(not (null (fboundp 'greet)))
=> T

;; An unbound symbol
(fboundp 'completely-undefined-function-xyz)
=> NIL

Testing setf Functions

fboundp also works with (setf name) function names to check if a setf function is defined.

;; Check if a setf function exists
(not (null (fboundp '(setf car))))
=> T

(defun my-accessor () nil)
(fboundp '(setf my-accessor))
=> NIL

(defun (setf my-accessor) (value)
(declare (ignore value))
nil)
(not (null (fboundp '(setf my-accessor))))
=> T

Guarding Against Undefined Functions

fboundp is useful for conditionally calling a function that may or may not be loaded.

;; Safely call an optional function
(when (fboundp 'optional-plugin-init)
(funcall 'optional-plugin-init))
=> NIL