Recursion in c

How Recursion works?.

Page 1 of 1

6 Replies - 1771 Views - Last Post: 31 December 2010 - 05:41 AM Rate Topic: -----

#1 vicckyjha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-December 10

Recursion in c

Posted 29 December 2010 - 07:09 AM

i have got a code which is as
#include<stdio.h>
void fun(int);
int main()
{
int a;
a=3;
fun(a);
printf("\n");
return 0;
}
void fun(int n)
{
if(n>0)
 {
  fun(--n);
  print("%d",n);  
  fun(--n);
  }
}


output is 0 1 2 0
plz explain this
Is This A Good Question/Topic? 0
  • +

Replies To: Recursion in c

#2 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Recursion in c

Posted 29 December 2010 - 07:20 AM

*
POPULAR

1. Post your code in code tags.
2. Have you tried to study anything about recursion, if not, then read these first,
http://www.dreaminco...d-a-discussion/
http://www.cprogramm...l/lesson16.html
and then come back with your queries if you still have any.

This post has been edited by newclearner: 29 December 2010 - 07:23 AM

Was This Post Helpful? 5
  • +
  • -

#3 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Recursion in c

Posted 30 December 2010 - 03:01 AM

*
POPULAR

As if giving negative rep to a random stranger on the net is going to help you. :bananaman:

This post has been edited by newclearner: 31 December 2010 - 01:28 AM

Was This Post Helpful? 5
  • +
  • -

#4 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Recursion in c

Posted 31 December 2010 - 01:28 AM

Ok, so - 1 in the above post again, but did that make you learn anything?
Have you become expert at C? :P
Was This Post Helpful? 0
  • +
  • -

#5 vicckyjha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-December 10

Re: Recursion in c

Posted 31 December 2010 - 01:41 AM

i just want to understand that when and how the first recursion fun(--n) breaks or comes to end inside if block as i know from factorial example that you have to force the recursion to end somewhere and i am not getting where and how recursion ends in above example waiting for reply with explanation?
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Recursion in c

Posted 31 December 2010 - 03:19 AM

So it took you all that time and aggravation to ask a meaningful question?
That's not a good use of your time and energy now is it?

View Postvicckyjha, on 31 December 2010 - 05:41 PM, said:

i just want to understand that when and how the first recursion fun(--n) breaks or comes to end inside if block

It doesn't.
If it encounters the breaking condition it never enters the if() block.
The if() test is the breaking condition.


View Postvicckyjha, on 31 December 2010 - 05:41 PM, said:

i am not getting where and how recursion ends in above example


Look at this
if(n>0)



That is the ending condition for the recursion.
Do you see how it works?
If not then ask a specific question that reflects that you have read the advice given to you by newclearner and made a serious effort to understand what is being said to you and what the code in front of you says.
No one is expecting you to understand what you are struggling to understand but people are expecting you to show that you doing all the work you can and asking for a 'leg-up' to get over the last little bit of the climb. If you seem to be asking for us to climb the wall for you then you have to be offering something to make the climb worth it.


View Postvicckyjha, on 31 December 2010 - 05:41 PM, said:

waiting for reply with explanation?


Can you out-wait patient bear?
http://www.dailyhaha...atient_bear.htm

With the sort of attitude you are giving out you are likely to find that help is hard to come by, both here and in meat-space.

Remember, you are asking people to do you a favour. You are asking people to give you their time and hard-won knowledge for free. Most of the people here sell their time and knowledge for substantial amounts of money most of the time. Getting access to it for free is a gift not to be sniffed at.

A little bit of 'nice' goes a long way when you are asking people to do that for you.
A bit of 'ugly' will also go a long way, but not in the direction you want to go.
Was This Post Helpful? 2
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2983
  • View blog
  • Posts: 10,309
  • Joined: 08-August 08

Re: Recursion in c

Posted 31 December 2010 - 05:41 AM

Hopefully you've learned something here, and since you now seem to be making an effort I'll say this:
fun(--n) decrements n and then calls the function with that decremented value. When the function returns, n still has the new lower value. That is very different from fun(n-1).

This post has been edited by CTphpnwb: 31 December 2010 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1