#include "MusimatTutorial.h"Go to the source code of this file.
Functions | |
| MusimatTutorialSection (B0125) | |
| Integer | recurFactorial (Integer x) |
| Static Void | para1 () |
| MusimatTutorialSection | ( | B0125 | ) |
Definition at line 2 of file B0125.cpp.
References para1().
00002 { 00003 Print("*** B.1.25 Recursive Factorial ***"); 00004 /***************************************************************************** 00005 00006 B.1.25 Recursive Factorial 00007 00008 Here is a more direct approach to computing factorials using recursion. Since 00009 x! = x * (x - 1)!, 00010 we can write a function that uses this expression directly, as shown below 00011 in the function recurFactorial(). 00012 *****************************************************************************/ 00013 para1(); // Step into this function to continue the tutorial 00014 }
| Static Void para1 | ( | ) |
Definition at line 23 of file B0125.cpp.
References recurFactorial().
00023 { 00024 /***************************************************************************** 00025 This method has two states. If x == 1, we return 1 since 1! is equal to 1. Otherwise, we return 00026 x multiplied by the factorial of x – 1. Consider the statement 00027 *****************************************************************************/ 00028 00029 Print(recurFactorial(5)); 00030 00031 /***************************************************************************** 00032 When the recurFactorial function is called, x is assigned the value 5. Because 5 is not equal to 1, the fac- 00033 torial function evaluates the Else statement and calls recurFactorial(4). Because 4 is not equal 00034 to 1, recurFactorial evaluates the Else statement and calls recurFactorial(3), and so on. 00035 Eventually, we reach recurFactorial(1), which returns 1, which is multiplied by 2, then by 3, then 00036 by 4, and finally by 5. The top-level recurFactorial() function returns the product, 120, to the 00037 Print() routine. 00038 00039 *****************************************************************************/ 00040 }}
| Integer recurFactorial | ( | Integer | x | ) |
1.4.7