Back to Blog

Clojure for Beginners: Clojure Functions with defn — The One Form You Need

Celest KimCelest Kim

Video: Clojure Functions with defn — The One Form You Need | Episode 4 by CelesteAI

Watch full page →

Clojure Functions with defn — The One Form You Need

Functions are Clojure's main building block. In this episode we cover defn — the one form you use to define every named function in your codebase. By the end you'll know parameters, return values, docstrings, and multi-arity, and you will have written a functions.clj file that ties them all together.

Code

;; Episode 4: Functions with defn
;; Clojure for Beginners in Neovim

;; A simple function — takes one argument, returns a string
(defn greet [name] (str "Hello, " name "!"))
(println (greet "Ada"))

;; Functions can take multiple arguments and return any value
(defn add [a b] (+ a b))
(defn square [x] (* x x))
(println "Sum:" (add 10 20))
(println "Square:" (square 9))

;; A docstring describes the function — it lives between name and args
(defn cube "Return x cubed." [x] (* x x x))
(println "Cube:" (cube 4))

;; Multi-arity — different bodies for different argument counts
(defn hello
  ([] "Hello!")
  ([name] (str "Hello, " name "!")))
(println (hello))
(println (hello "World"))

Key Points

Watch the video above for a full walkthrough — every keystroke is shown so you can code along.

Student code: GitHub