- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/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         para1(); // Step into this function to continue the tutorial
    00013 }
    00014
    00015 Integer factorial(Integer x) {
    00016         Integer n = 1;
    00017         For (Integer i = x; i > 1; i = i - 1)
    00018                 n = n * i;
    00019         Return(n);
    00020 }
    00021
    00022 Static Void para1() {
    00023 /*****************************************************************************
    00024  The statement
    00025  *****************************************************************************/
    00026
    00027 Print(factorial(5));
    00028
    00029 /*****************************************************************************
    00030  prints 120.
    00031
    00032 We start with n = 1 and i = 5. The For loop takes the previous value of n, multiplies it by the
    00033 current value of i and reassigns the value to n. It then decrements i and performs the operation
    00034 repeatedly so long as i > 1.
    00035
    00036 *****************************************************************************/
    00037 }}
    00038
    00040 /* $Revision: 1.1 $ $Date: 2006/09/08 19:11:32 $ $Author: dgl $ $Name:  $ $Id: _b0124_8cpp-source.html,v 1.1 2006/09/08 19:11:32 dgl Exp $ */
    00041 // The Musimat Tutorial © 2006 Gareth Loy
    00042 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00043 // and published exclusively by The MIT Press.
    00044 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00045 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00046 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00047 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00048 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00049 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00050 // The Musimat website:                 http://www.musimat.com/
    00051 // This program is released under the terms of the GNU General Public License
    00052 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00053