-
Notifications
You must be signed in to change notification settings - Fork 2
Clojure Functions
Functions! They're pretty important. It's very difficult to do anything without a function. They're integral to any language, but especially Clojure, since it's a functional programming language that rejects object-oriented design. Let's learn some more about them!
Arity refers to the number of arguments that your function expects.
;; add expects 2 arguments. Its arity is 2.
(defn add [x y] (+ x y))
(add 2 2)
; => 4
;; + itself is a function, and it can have any number of arguments.
(+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) ;; and so on...
; => 136
Clojure has some special syntax that allows you to let your function do different stuff depending on the number of arguments it receives. This is called variable arity.
(defn foo
([] ; if this function gets no arguments...
(println "Lisa needs braces!")) ; do this.
([arg1] ; if this function gets 1 argument...
(println "Dental plan!"))) ; do this instead!
(foo)
; => Lisa needs braces!
; nil
(foo "this is a placeholder argument.")
; => Dental plan!
; nil
Let's look at a really simple function: a function that adds 1 to a number.
;; I've called this function "my-inc" so you don't confuse it with inc.
;; inc is a built-in function that already does this for us.
(defn my-inc [n] (+ 1 n))
(inc' 5)
; => 6
This looks pretty simple. It takes a single parameter - n
- and returns n + 1
. Let's pick it apart.
(def my-inc-2 (fn [n] (+ 1 n)))
(inc' 5)
; => 6
You can see from this that using defn
is just shorthand for using (def ... (fn ...))
. But this reveals something interesting. What we're actually doing isn't 'defining a function', it's just binding an anonymous function to a special name - inc'
. What if we don't give it a name?
((fn [n] (+ 1 n)) 5)
; => 6
Bam! Boom! Kapow! Anonymous functions. This might seem useless now, but it comes in pretty handy later on for applying functions to lists using map
, reduce
and filter
. Giving every function you write a name gets boring and cumbersome, fast.
There's a shorter way to write anonymous functions, intended for very short, simple functions. It does not allow for destructuring or variable arity. However, it is quite concise.
(#(+ 1 %) 5)
; => 6
#(...)
is a shorthand way to define an anonymous function. %
refers to the first argument to the function. If your function expects several arguments, you can use %1, %2, ... %n
.
(#(str %1 %2 %3) "foo" "bar" "baz")
; => "foobarbaz"
👈 Previous | 📖 Home 📖 | Next 👉 |
---|---|---|
Loop and Recur | Table of Contents | Collections |
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links