Reputation: 45
I'm a begginer with clojure and i were reading a book that contained that function:
(defn illustrative-function [] (+ 1 304) 30 "joe")
And if you invoke that function without an argument it returns only "joe", as you can see:
(illustrative-function )
=> "joe"
My question:
Why the numbers inside the function aren't printed? 305 and 30? Even when i use and argument (and a parameter) it doesn't return nothing:
(defn illustrative-function [x] x (+ 1 304) 30 "joe")
=> #'dev-projects.core/illustrative-function
(illustrative-function 3)
=> "joe"
(illustrative-function [3])
=> "joe"
Can someone explain why does that happens?
Why the integers and the arguments are not printed?
Thanks!
Upvotes: 0
Views: 60
Reputation: 37008
The body of a defn
is wrapped in an do
. do
allows you to execute
multiple instructions usually for side-effects. The last statement in
the do
is the return. So this looks like this:
(defn illustrative-function
[]
(do
(+ 1 304) ; no-op
30 ; no-op
"joe")) ; return
(+ 1 304)
is read and calculated, then thrown aways. 30
is just
read and thrown away. "joe"
then is the return of do
and by that
the return of the function.
If you want to "print" something, you have to explicitly print
it.
E.g. replace 30
with (println 30)
. Also note, that in the REPL, the
return value is printed into the REPL - if you call that function from
somewhere else, nothing is printed unless you print explicitly.
Upvotes: 2