philipbohun.com

Glojure by Example: Functions

defn names a function. Parameters are a vector. The last expression is the return value. There is no return keyword.

(defn add [a b]
  (+ a b))

(println (add 2 3))

A function can have several arities. Each arity is a parameter vector and a body. One arity can call another.

(defn greet
  ([name] (greet "hello" name))
  ([salutation name]
   (str salutation ", " name)))

(println (greet "Ada"))
(println (greet "hi" "Ada"))

fn makes an anonymous function. let can name it for the surrounding body.

(let [double (fn [n] (* n 2))]
  (println (double 21)))

The value of a function is its last expression.

$ glj functions.glj
5
hello, Ada
hi, Ada
42

Next example: Destructuring.