You can express most of the problems in the following program by using recursion. We represent the function add by using recursion.
Program
#include int add(int pk,int pm); main() { int k ,i,m; m=2; k=3; i=add(k,m);. printf("The value of addition is %d ",i); } int add(int pk,int pm) { if(pm==0) return(pk); \ A else return(1+add(pk,pm-1)); \ B }
Explanation
add (x, y) = 1 + add(x, y-1) y > 0 = x y = 0 for example, add(3, 2) = 1 + add(3, 4) add(3, 1) = 1 + add(3, 0) add(3, 0) = 3 add(3, 1) = 1+3 = 4 add(3, 2) = 1+4 = 5
If you analyze the address of local variables of the recursive function, you will get two important results: the depth of recursion and the stack overheads in recursion. Since local variables of the function are pushed into the stack when the function calls another function, by knowing the address of the variable in repetitive recursive call, you will determine how much information is pushed into the stack. For example, the stack could grow from top to bottom, and the local variable j gets the address 100 in the stack in the first column. Suppose stack overheads are 16 bytes; in the next call j will have the address 84, in the call after that it will get the address 16. That is a difference of 16 bytes. The following program uses the same principle: the difference of the address in consecutive calls is the stack overhead.
Program
#include int fact(int n); long old=0; \E long current=0; \F main() { int k = 4,i; long diff; i =fact(k); printf("The value of i is %d ",i); diff = old-current; printf("stack overheads are %16lu ",diff); } int fact(int n) } int j; static int m=0; if(m==0) old =(long) &j; \A if(m==1) current =(long) &j; \B m++; \C printf("the address of j and m is %16lu %16lu ",&j,&m); \D if(n<=0) return(1); else return(n*fact(n-1)); }
Explanation
Note |
You can also check whether the address of m is constant. |
Points to Remember
A recursive function is a function that calls itself. Some problems can be easily solved by using recursion, such as when you are dividing a problem into sub- problems with similar natures. Note that recursion is a time-consuming solution that decreases the speed of execution because of stack overheads. In recursion, there is a function call and the number of such calls is large. In each call, data is pushed into the stack and when the call is over, the data is popped from the stack. These push and pop operations are time-consuming operations. If you have the choice of iteration or recursion, it is better to choose iteration because it does not involve stack overheads. You can use recursion only for programming convenience. A sample recursive program for calculating factorials follows.
Program
#include int fact(int n); main() { int k = 4,i; i =fact(k); \ A printf("The value of i is %d ",i); } int fact(int n) { if(n<=0) \ B return(1); \ C else return(n*fact(n-1)); \ D }
Explanation
fact (5) = 5 * fact (4) In general, fact (N) = N * fact (N-1) fact 5 is calculated as follows: fact (5) = 5 * fact (4) i.e. there is call to fact (4) \ A fact (4) = 4 * fact (3) fact (3) = 3 * fact (2) fact (2) = 2 * fact (1) fact (1) = 1 * fact (0) fact (0) = 1 \ B fact (1) = 1 * 1, that is the value of the fact(0) is substituted in 1. fact (2) = 2 * 1 = 2 fact (3) = 3 * 2 = 6 fact (4) = 4 * 6 = 24 fact (5) = 5 * 24 = 120 \ C
fact ( N) = N * fact (N-1) N > 0 = 1 N = 0
Points to Remember
Part I - C Language
Part II - Data Structures
Part III - Advanced Problems in Data Structures