473,503 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursion doubt:-

48 New Member
Expand|Select|Wrap|Line Numbers
  1. {
  2.  int a;
  3.  a=3;
  4.  fun(a);
  5.   system("PAUSE");    
  6.   return 0;
  7. }
  8. void fun(int n)
  9. {
  10.      if (n>0)
  11.      {
  12.              fun(--n);
  13.              printf("%d",n);
  14.              fun(--n);
  15.              }
  16.              }
  17.  
  18.  
What does this program prints....
1. At first 3 is passed to fun
2. if checks 3>0 since it is true control goes inside the loop
3. In the loop again fun(==3) so fun(2), again if(2>0).
4. Likewise if(0>0) will be false and atlast the program will not print anything.

But my compiler provides the answer as 0120

Kindly provide the clarification. Thanks in advance
Sep 2 '10 #1
5 1106
whodgson
542 Contributor
which loop?
Sep 2 '10 #2
whodgson
542 Contributor
2. if checks 3>0 since it is true control goes inside the loop
Which loop?
Sep 2 '10 #3
vensriram
48 New Member
The IF loop that is inside the function FUN.That loop has the call for the function FUN again....
Sep 2 '10 #4
vensriram
48 New Member
Yes it goes inside the loop. But again there is a function call inside the loop fun(--n); so this interprets in to fun(2). So this is a recursive call so the control again goes to the start of the function fun() so again it it checks if(2>0) and again since it is true it goes inside the loop. Again the same thing continues until IG(0>0) is encountered. now the if becomes false and hence the control should exit.
Hence it should not print anyhing. But it prints 0120.
Kindly clarify
Sep 2 '10 #5
MartijnHoekstra
39 New Member
First of, it is called an IF statement.
Loops are 'while' or 'for'.

Your clarification started correctly, firstly you call fun(3). which imediatly calls fun(2) -> fun(1) then fun(0).
The last one fun(0), doesnt do anything.
so now it goes back to where it called fun(0).. below there is the printf..
You forgot that part.
Also remember, every time fun is called, a new 'n' is created for every 'call stack'. So when coming back to previous call, the n has different value.
Sep 2 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3398
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
2735
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
12
3709
by: Mikito Harakiri | last post by:
I wonder if WITH RECURSIVE MaryAncestor(anc,desc) AS ( (SELECT parent as anc, child as desc FROM ParentOf WHERE desc = "Mary") UNION (SELECT A1.anc, A2.desc FROM MaryAncestor A1, MaryAncestor...
43
4116
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
75
5538
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
13
2086
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
20
2959
by: athar.mirchi | last post by:
..plz define it.
35
4682
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.