[an error occurred while processing this directive] [an error occurred while processing this directive]                    Modeling and Abstraction
Rice University
COMP 200
Elements of Computer Science
 
Function Composition


When we write a function to solve a problem, we do not usually write it from scratch but combine existing functions to obtain the desired solution.  For example, take a look at the function to compute the area of a circle of a given radius.

(define (circleArea radius)
  (* pi (sqr radius))

The above function makes use of two predefined functions: the multiplication operation (*) and the square function (sqr).  The multiplication function takes two numbers as inputs, while the sqr function only needs one input.  What we are doing in the above formula is to feed the radius as input into the sqr function and feed the output of the sqr function into one of the inputs of the * function.  This is an example of what is called function composition.

Another example is the area of a ring given an outer radius and an inner radius.  This is done by composing the subtraction operation with the circleArea function.

 


© Dung X. Nguyen

Last revised 09/21/2008 10:11 PM

Maintained by the professor; see contact information on the course home page