explain me this code...

#include<stdio.h>
int call(int);
int main()
{
int n=9;
printf(“1 function”);
call(n);
getchar();
return 0;
}
int call(int n)
{
if(n<=0)
return 0;
else
call(–n);
printf("\n%d function",n);
call(–n);
return 0;
}

take a look at this link…and pls ask a more specific doubt…is it like you did not understand the flow or like what???

EDIT

i will explain for n=4!!!

1) call(4)//through main

2) in call(4)
	
	call(--4) i.e. call(3)

3) in call(3)

	call(--3) i.e. call(2)

4) in call(2)

	call(--2) i.e. call(1)

5)

	call(--1) i.e. call(0)

6) in call(0)

	return 0 i.e. return to call() in the step 5

7) print(n) i.e. 0 and then call (--0) i.e. call(-1)


8) in call(-1)

	return 0 i.e. return to call in 7th step

9) return from 7th step to call in 4th step

10) print(n) i.e. 1 and then call(--1) i.e. call(0)

11) in call(0)

	return 0 i.e. return to 4th step

12) return from that to 3rd step

	print(n) i.e. 2 and then call(--2) i.e. call(1)

13) in call(1)

	call(--1) i.e. call(0)

14) in call(0)

	return 0 i.e. return to step 13

15) print 0 and call(--0) i.e. call(-1)


16) return 0 i.e. return to 13th step from there return to  step 12 hen return to step 3 then return to step 2

17) print(n) i.e. 3 and then call(--3) i.e. call(2)

18) in call(2)...call(--2) i.e. call(1)

19) in call(1)...call(--1) i.e. call(0)

20) in call(0) return 0 i.e. return to step 19

21) print 0 and call(--0)..i.e. call(-1) it will return 0 then from step 19 return to step 18

22) print 1 and call(--1)..i.e. call(0) it will return 0 then from step 18 return to step 17 then to step 2 and then to step 1...and that is your output!!!!

0
1
2
0
3
0
1

pls dont ask for a higher number my brain’s Stack will overflow…:stuck_out_tongue:

hope it helps…:slight_smile:

2 Likes

@kunal361:abt the flow

was trying to explain by making a diagram but was getting confused…so had to write down…hope u understand…if not then i’ll try to draw the diagram using the above steps!!!

haha:)…i understood…thank u @kunal361

1 Like