+ 3
C program...... can anyone tell me how this program works?
4 Antworten
+ 6
https://code.sololearn.com/cb28eXLo0j6g/?ref=app
+ 13
0)f(3) called
then f(--3) that is f(2) called in f(3)
then f(--2) that is f(1) called in f(2)
then f(--1) that is f(0) called in f(1) but nothing printed as statements in if() not get executed
1)then it go back to f(1) as f(0) is over, n=0 after f(--1)
printf("%d",n); //print"0"
then f(-1) get called, nothing printed
2)now it go back to f(2) as f(1) is executed completely & n=1 now after f(--2)
printf("%d",n) //print "1"
then f(--1) is called, nothing printed for f(0)
3)now it go back to f(3) as f(2) is executed completely & n=2 now after f(--3)
print("d",n) //prints "2"
then f(--2) that is f(1) is called again, then in f(1) :
a) f(--1) or f(0) --> print nothing
b) printf("d",n); //prints "0"
c) f(--0) that is f(-1) --> prints nothing
4)f(3) is executed completely & it ends here
+ 1
actually the recursive programs are managed with a implicit stack which holds the function calls sequence.
To understand such multiple recursive programs, try it on paper with recursive tree method.
Parent node is the calling function and the child nodes are the functions which are called by the parent.
For every value of n, extend the tree and try to understand the how smartly a compiler executes such programs.
Hope this is helpful🙂
0
this program uses a function f....which is a recusrive function and calls itself which executing.....the condition for the recession to stop given in the if condition is that the number becomes 0....so the function keeps calling itself untill the value of the given argument becomes zero