What's going on? Scheme evaluated the expression that accompnaies
fraconce, when it was first defined. Hence,
since the expression had a particular value once, it retains that
value forever.

What we'd really like to do is to say that ``frac
is a procedure that takes a value as an input and returns
an appropriate fraction''. You know that Scheme has procedures, since
you've used lots of built-in procedures, including sqrt,
*, cons, and list. But can you
define your own procedures? Yes.

Convention in Scheme (and all programming languages) is that we carefully
document what our procedures do, including input values, output values,
and assumptions. We use comments provide information to the reader of
our program (that is, to people instead of the computer). In Scheme,
comments begin with a semicolon and end with the end of the line.

;;; Samuel A. Rebelsky
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; rebelsky@cs.grinnell.edu
;;; Procedue:
;;; square
;;; Parameters:
;;; val, a number
;;; Purpose:
;;; Compute val*val
;;; Produces:
;;; The result of the computation
;;; Preconditions:
;;; val must be a number
;;; Postconditions:
;;; The result is the same "type" of number as val (e.g., if
;;; val is an integer, so is the result; if val is exact,
;;; so is the result).
;;; Citations:
;;; Based on code created by John David Stone dated March 17, 2000
;;; and contained in the Web page
;;; http://www.math.grin.edu/~stone/courses/scheme/procedure-definitions.xhtml
;;; Changes to
;;; Parameter names
;;; Formatting
;;; Comments
(define square
(lambda (value)
(* value value)))

At times, we'll want to write procedures that take more than one
parameter. Such procedures look just like procedures with one
parameter, except that you can list more parameters between the
parentheses.

(lambda (param1, param2 ... paramn)
expression
)

For example, here is a simple procedure that finds the average of two
nubmers

;;; Samuel A. Rebelsky
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; rebelsky@cs.grinnell.edu
;;; Procedure:
;;; pairave
;;; Parameters:
;;; val1, an exact number
;;; val2, an exact number
;;; Purpose:
;;; Compute the average of two numbers.
;;; Produces:
;;; The average of those two numbers.
;;; Preconditions:
;;; Both val1 and val2 are exact numbers.
;;; Postconditions:
;;; The result is an exact number. The result is equidistant
;;; from val1 and val2.
(define pairave
(lambda (val1 val2)
(/ (+ val1 val2) 2)))

Disclaimer:
I usually create these pages on the fly. This means that they
are rarely proofread and may contain bad grammar and incorrect details.
It also means that I may update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Thu May 3 23:10:34 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/procedures.html.
You may validate
this page's HTML.
The source was last modified Wed Jan 31 08:54:37 2001.