Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


7-6 Recursion

A function or procedure may call itself, a property called recursion. The factorial function is a good example of recursion. You know (from Chapter 4) that the factorial of a number “X” can be obtained from the relation:

    factorial(X) = X * factorial(X-1)

In other words, to get the factorial of “4” you multiply “4” by the factorial of “3”; to get the factorial of “3” you multiply “3” by the factorial of “2,” etc. This continues until you reach the value “1.”

Here is the program that contains the factorial function.

{ -------------------- figure 7-8 -------------------- }
PROGRAM FunctionRecursion(INPUT, OUTPUT);
VAR
 A:INTEGER;
{ ----------- Function Definition ------------ }
FUNCTION Factorial(X:INTEGER):REAL;
BEGIN
 IF X <= 1 THEN
   Factorial:= 1
 ELSE
   Factorial:= X * Factorial(X-1);
END;
{ --------------- End of Function ---------------- }
{ ----------------- Main program ----------------- }
BEGIN
 WRITE('Enter a number: ');
 READLN(A);
 WRITELN('The Factorial of ', A,' = ', Factorial(A):0:0)
END.

Sample run:

Enter a number: 6
The Factorial of 6 = 720

Notice in the function program that in the statement:

    Factorial:= X * Factorial(X-1);

the left side contains the name of the function, while in the right side there is a call of the function to calculate the factorial of “X-1.” This process will continue until the condition terminates the function.

Drill 7-3

Write the factorial subprogram as a procedure and compare it to the factorial function.

Summary

In this chapter you learned about the Pascal program structure.

You know how to divide your program into subprograms, whether functions or procedures. These are important points to remember:

1.  A subprogram is declared in the last section of the declaration part and consists of a header, a declaration part, and statements.
The header of a procedure takes the form:
    PROCEDURE name;
    or
    PROCEDURE procedure-name( formal-parameter-list);

The header of a function takes the form:
    FUNCTION function-name( formal-parameter-list): return-type;
2.  A procedure is called by its name exactly like a statement.
When parameters are used in a procedure call, they must match the parameters in the procedure header. Procedure parameters are either value or variable parameters. A variable parameter is used when it is required to have the procedure change the value of the parameter.
3.  A function is usually called as part of an expression; it returns a single value that replaces the name of the function in that expression.
4.  You now know that each variable has a scope, and you learned the rules that control the scope and the relationship between global and local variables.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.