Functions

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

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0124)
Integer factorial (Integer x)
Static Void para1 ()

Function Documentation

Integer factorial ( Integer  x )

Definition at line 16 of file B0124.cpp.

                             {
        Integer n = 1;
        For (Integer i = x; i > 1; i = i - 1) {
                n = n * i;
        }
        Return(n);
}
MusimatTutorialSection ( B0124   )

Definition at line 2 of file B0124.cpp.

References para1().

                              {
        Print("*** B.1.24 Recursion ***");
        /*****************************************************************************
         
         B.1.24 Recursion
         
         Recursion means referring back to a value we've calculated previously. Consider the factorial 
         operation where 5! means 5 x 4 x 3 x 2 x 1. We could use a For statement to calculate factorials. 
         This function calculates factorials using iteration:
         *****************************************************************************/
        
        para1(); // Step into this function to continue the tutorial
}
Static Void para1 (  )

Definition at line 24 of file B0124.cpp.

References factorial().

                    {
        
        /*****************************************************************************
         The statement
         *****************************************************************************/
        
        Print(factorial(5));
        
        /*****************************************************************************
         prints 120.
         
         We start with n = 1 and i = 5. The For loop takes the previous value of n, multiplies it by the 
         current value of i and reassigns the value to n. It then decrements i and performs the operation 
         repeatedly so long as i > 1.
         
         *****************************************************************************/
}