Browse Clojure Foundations for Java Developers

def vs defn

def creates a var; defn is the idiomatic way to define a named function.

In Clojure, def creates a var in the current namespace. A var can hold any value: a number, a map, a function, a Java object, etc.

defn is the idiomatic way to define a named function. Conceptually, it is a def whose value is an fn, with some extra conveniences (name, docstring, arglists).

1(def answer 42)
2(defn add [a b] (+ a b))

Why this matters in practice

  • You will see a lot of “top-level defs” in Clojure namespaces; that is how APIs are defined.
  • At the REPL, redefining vars is normal and is part of the workflow.

Java mental model: vars are closer to namespace-scoped bindings than to class fields. They support interactive development and late binding.

In this section

  • Using `def` for Definitions
    Learn what `def` actually creates, when it belongs in a namespace, and why it is not the same as a mutable local variable.
  • Defining Functions with `defn`
    Use `defn` as the normal way to publish named functions, and understand what it adds beyond `def` plus `fn`.
  • Scope and Immutability
    Distinguish namespace vars from local bindings, and learn where Clojure's immutability guarantee actually applies.
Revised on Friday, April 24, 2026