函數(shù)通過使用'defn'宏來定義。 以下是函數(shù)定義的一般語法。
(defn functionname “optional documentation string” [arguments] (code block))
函數(shù)可以有文檔字符串,這很好描述函數(shù)實際上做什么。
下面是一個簡單的函數(shù)示例。
(ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def x 1) (def y 1.25) (def str1 "Hello") (println x) (println y) (println str1)) (Example)
在上面的例子中,函數(shù)的名稱是Example。
以上示例將輸出以下結果:
1 1.25 Hello
更多建議: