• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0124.cpp

Go to the documentation of this file.
00001 #include "MusimatTutorial.h"
00002 MusimatTutorialSection(B0124) {
00003         Print("*** B.1.24 Recursion ***");
00004         /*****************************************************************************
00005          
00006          B.1.24 Recursion
00007          
00008          Recursion means referring back to a value we've calculated previously. Consider the factorial 
00009          operation where 5! means 5 x 4 x 3 x 2 x 1. We could use a For statement to calculate factorials. 
00010          This function calculates factorials using iteration:
00011          *****************************************************************************/
00012         
00013         para1(); // Step into this function to continue the tutorial
00014 }
00015 
00016 Integer factorial(Integer x) {
00017         Integer n = 1;
00018         For (Integer i = x; i > 1; i = i - 1) {
00019                 n = n * i;
00020         }
00021         Return(n);
00022 }
00023 
00024 Static Void para1() {
00025         
00026         /*****************************************************************************
00027          The statement
00028          *****************************************************************************/
00029         
00030         Print(factorial(5));
00031         
00032         /*****************************************************************************
00033          prints 120.
00034          
00035          We start with n = 1 and i = 5. The For loop takes the previous value of n, multiplies it by the 
00036          current value of i and reassigns the value to n. It then decrements i and performs the operation 
00037          repeatedly so long as i > 1.
00038          
00039          *****************************************************************************/
00040 }
00041 
00043 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name:  $ $Id: B0124.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */
00044 // The Musimat Tutorial © 2006 Gareth Loy
00045 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00046 // and published exclusively by The MIT Press.
00047 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00048 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00049 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00050 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00051 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00052 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00053 // The Musimat website:                 http://www.musimat.com/
00054 // This program is released under the terms of the GNU General Public License
00055 // available here:                      http://www.gnu.org/licenses/gpl.txt
00056 

Generated on Fri Nov 26 2010 16:18:25 for Musimat Tutorial by  doxygen 1.7.2