目录

Defining a Function

通过使用'defn'宏来定义函数。 以下是函数定义的一般语法。

语法 (Syntax)

(defn functionname
   “optional documentation string”
   [arguments]
   (code block))

函数可以有文档字符串,这很好地描述了函数实际执行的操作。

例子 (Example)

以下是一个函数的简单示例。

(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)

在上面的示例中,函数的名称是Example。

输出 (Output)

1
1.25
Hello
↑回到顶部↑
WIKI教程 @2018