Function definition skeleton
A function definition tells Scheme the name of a new function
and how it works. The skeleton of a function definition is a
first draft, essentially translating the contract into Scheme syntax but not specifying
how the function does its job.
It has the following form:
(define (function-name parameter-name)
... parameter-name ... )
You can also write functions with multiple parameters:
(define (function-name parameter-name other-parameter-name)
... parameter-name ... other-parameter-name ... )
In either case, the definition consists of several parts,
each separated by a space (and anywhere
you can put a space, you can also put a new line):
- "(define ("
- The name of the function
- The name by which you want to refer to the parameter (if there are
more than one, they must be separated by spaces)
- A right parenthesis ), matching the one after
define
- The body of the function (for now, just
put the parameter name(s) again, surrounded by
"..." ellipses)
- A right parenthesis ), matching the one before
define
The first four of these are collectively called the
header, and traditionally go on one line, with the
body starting on the next line.
Example:
(define (cube number)
... number ... )
Note that the function-name and parameter-name can
each be whatever you want, as long as each is a sequence of letters (and
perhaps some punctuation marks, but no spaces). Typically the
function-name is chosen to suggest what the function does, and
the parameter-name is chosen to suggest the parameter's type or
role.
Last modified:
Thu Jun 1 14:37:48 EDT 2000
Stephen Bloch / sbloch@adelphi.edu