Page 159 - DCAP407_DATA_STRUCTURE
P. 159

Data Structure



                                         count (val);
                                          }
                                         Output:
                                         Count till what value? 4
                                         Start counting at level 1: val =4
                                         Start counting at level 2: val =3
                                         Start counting at level 3: val =2
                                         Start counting at level 4: val =1
                                         Stop counting at level 4: val =1
                                         Stop counting at level 3: val =2
                                         Stop counting at level 2: val =3
                                         Stop counting at level 1: val =4
                                         In this example:
                                         1.  First, the stdio.h header file is included, using the include keyword.

                                         2.  Then, an integer variable named level is declared.
                                         3.  Then, a function named count is declared.
                                         4.  In the count() function,
                                             (a)  First, increment the level value and print the values of level and val.
                                             (b)  Then, check the value of val using an ‘if’ condition to determine whether
                                                 it is greater than 1. If the condition is true, a recursive call is made by
                                                 passing the value (val-1).
                                             (c)  Then, print the value of val.
                                             (d)  Finally, decrement the value of level and print the values of level and
                                                 val.
                                         5.  Execution begins at the function main () which does not return any value.
                                         6.  In the main() function,
                                             (a)  First, initialize an integer variable val.
                                             (b)  Then, declare a function named count.
                                             (c)  Then, accept an integer value and store it in val.

                                             (d)  Then, set the value of level to zero.
                                         Finally, call the count function using the parameter val.
                          Now let us understand how the recursion process functions. The program starts executing in the main
                          function. Consider that the function count () has val = 4. The count (4) function starts executing while
                          the main function is on hold. This function exhibits the level information which is nothing but the level
                          of recursion. As value of val is equal to 4, which is greater than 1, the function count () is called after
                          decrementing val to 3. Then count (3) exhibits details about the level of recursion. As value of val is
                          equal to 3, which is greater than 1, the function count () is once again called with val=2.
                          At this point, the main (), count (4) and count (3) are all on hold, and count (2) starts executing. Similar
                          to the previous calls, count (2) exhibits details about the level of recursion. Then, the function checks
                          whether its val value is greater than 1. As the condition is true, the main (), count (4), count (3), and
                          count (2) are on hold,  while  count (1) starts executing. count (1) exhibits details about the level of
                          recursion. Then the ‘if’ condition fails and control shifts to the next statement, i.e., to printf () statement
                          to display the value of val.
                          The best feature of the recursive function call is that, the last version called is the first to be performed.
                          Also, the last one called is the first to finish its task. We can see this in the output shown.






                          152                     LOVELY PROFESSIONAL UNIVERSITY
   154   155   156   157   158   159   160   161   162   163   164