To declare a new function use define
,
which has the following form:
(define (function-name
parameter-names
)body
)
This creates a new function named function-name
,
which takes parameter-names
as parameters.
When the function is called, the parameter-names
are initialized with the actual arguments. Then body
is evaluated, and that becomes the result of the call.
For example in the factorial
function we looked at recently,
the function-name
is factorial
,
and the parameter-names
is x
:
(define (factorial x) (if (< x 1) 1 (* x (factorial (- x 1)))))
You can declare a function that takes optional argument, or variable number of arguments. You can also use keyword parameters. Read more here.