The define function is used to define functions. For format of define is:
(define (name args) body)Where args is a list of arguments arg1 arg2 ... argN, and body is the body of the function. When the function is called, via (name arg1 arg2 ... argN), the actual arguments in the call are bound to the formal arguments in the define, which act as local variables within body. For example, the ever-popular factorial function would be defined as follows (n factorial is equal to , and 0 factorial is defined to equal 1):
(define (fact n) (cond ((= n 0) 1) (else (* n (fact (- n 1))))))A call to this function with n equal to 3 proceeds as follows:
(fact 3)A function with a variable number of arguments is defined as follows:(* 3 (fact 2))
(* 3 (* 2 (fact 1)))
(* 3 (* 2 (* 1 (fact 0))))
(* 3 (* 2 (* 1 1)))
6
(define (name args . rest) body)Within body, the arguments arg1 arg2 ... argN are bound to the first N arguments to the function, and any remaining arguments are bound as a list to rest.