Page 171 - DCAP407_DATA_STRUCTURE
P. 171

Data Structure



                                          void main ()
                                          {
                                              /* Initialise an integer variable num */
                                              int num;
                                               printf (“Enter the number: \n”);
                                              /* Accept an integer value num */
                                              scanf (“%d”, &num);
                                              /* Print the value of fact (num) */
                                              printf (“ The factorial of %d = %d\n”, num, fact (num));
                                          }

                                          Output:
                                          Enter the number: 5
                                          The factorial of 5 = 120

                                          In this example:
                                          1.   First, the stdio.h header file is included using the include keyword.
                                          2.   Then, a recursive function fact is declared which takes an integer variable
                                               num and returns an integer value.
                                          3.   In this method,
                                               (a)  The value of  num  is checked using  an  ‘if’  condition to determine
                                                  whether it is equal to 0. If the condition is true, value 1 is returned.
                                               (b)  Otherwise, the fact function invokes  itself  by  return  (num * fact
                                                  (num-1)).
                                          4.   The main () program is then defined. It does not return any value.
                                          5.   In main ():
                                               (a)  First, an integer variable num is initialized.
                                               (b)  Then, an integer value num is accepted.

                                               (c)   Finally, the function fact (num) is invoked and the value is printed
                                                  and returned.

                          Suppose the value of ‘num’ is 4 when fact (num) is executed, then the value of ‘num’  along with a
                          return address of say 1000, stored in the program counter is pushed onto the stack. Table 9.1 (a) depicts
                          the stack at this point. As ‘num’ is not zero, (num-1) is set to 3. fact () function is invoked with (num-1)
                          as the parameter. Prior to entering into the fact () function, assume that the return address is 2000 and
                          the local parameters are pushed onto the stack as depicted in table 9.1 (b). Each time the fact () function
                          is invoked recursively, a new set of local parameters will be pushed onto the stack. When fact () is
                          invoked recursively till the value of ‘num’ becomes zero, the value of num is decremented by 1. Table
                          9.1 (a) through 9.1 (e) depicts the stack every time the fact () function is called.
                          When the value of ‘num’ becomes zero, the return statement with value 1 is returned. This is termed as
                          base or terminal condition. Once the return statement executes, the top most stack variables are popped
                          and the control moves to the original point from where it was invoked and value 1 is copied into fact
                          (num-1).










                          164                     LOVELY PROFESSIONAL UNIVERSITY
   166   167   168   169   170   171   172   173   174   175   176