Browse Clojure Foundations for Java Developers

Functions and Anonymous Functions

How to define and use functions in Clojure: arities, anonymous functions, variadic args, destructuring, and apply.

Functions are Clojure’s unit of composition. If you’re used to Java, think: a function is like a static method that you can pass around as a value.

Define Functions with defn

1(defn greet
2  "Returns a greeting string."
3  ([name] (str "Hello, " name "!"))
4  ([] (greet "world")))
5
6(greet "Ada") ; => "Hello, Ada!"
7(greet)       ; => "Hello, world!"

Notes that matter in real code:

  • the last expression is the return value
  • multiple arities are common (dispatch by argument count)

Anonymous Functions: fn and #()

Prefer fn when the function is more than a one-liner:

1(map (fn [n] (* n n)) [1 2 3])
2;; => (1 4 9)

Use #() for short transforms:

1(map #(* % %) [1 2 3])
2(map #(+ %1 %2) [1 2] [10 20])
3;; => (11 22)

Variadic Args and apply

Use & to accept “the rest” of the arguments:

1(defn sum [& xs]
2  (reduce + 0 xs))

Use apply when you have arguments in a collection:

1(apply + [1 2 3]) ; => 6

Destructuring (Readable Parameter Parsing)

Map destructuring pulls keys out directly, and :or lets you supply defaults:

1(defn area [{:keys [w h] :or {w 0 h 0}}]
2  (* w h))
3
4(area {:w 3 :h 4}) ; => 12
5(area {:w 3})      ; => 0

Vector destructuring is useful when a parameter is “position-based”:

1(defn first-two [[a b]] [a b])

Closures: Functions Capture Values

1(defn make-adder [n]
2  (fn [x] (+ x n)))
3
4(def add10 (make-adder 10))
5(add10 5) ; => 15

Knowledge Check: Functions

### Given the `greet` function above, what does calling `(greet)` return? - [x] `"Hello, world!"` - [ ] `"Hello, "` - [ ] `"Hello, nil!"` - [ ] It throws because Clojure requires an argument. > **Explanation:** Clojure functions can have multiple arities. The zero-arg arity delegates to the one-arg arity. ### What does `(apply + [1 2 3])` do? - [x] Calls `+` with `1`, `2`, and `3` as separate arguments and returns `6`. - [ ] Adds the vector to `+` and returns `[1 2 3]`. - [ ] Mutates the vector and returns `nil`. - [ ] Only works for Java varargs methods, not Clojure functions. > **Explanation:** `apply` spreads a collection into positional arguments for a function call. ### In the `area` example, what does `{:keys [w h] :or {w 0 h 0}}` mean? - [x] Pull `:w` and `:h` from the map into locals `w`/`h`, defaulting missing values to `0`. - [ ] Create a new map with keys `:w` and `:h`. - [ ] Force the caller to pass both `:w` and `:h` or throw. - [ ] Convert `w` and `h` into keywords. > **Explanation:** Destructuring is “parameter parsing”: it binds locals from structured inputs, and `:or` supplies default values.
Revised on Friday, April 24, 2026