Lost Password?

  #1 (permalink)  
Old 03-18-2008, 11:57 PM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 5
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default recursion

Recursion is actually the easy way to do things. But, recursion is
often explained poorly, and explained in a way that makes it seem hard,
so people think it's hard. Deriving a logic for recursive iteration sometimes seems a bit out of the way because it's not linear. It is totally disconnected from our real life and there is no job we do recursively.

Here i have tried to explain recursion in a simple way. Recursion is the call of a function within the function itself thereby putting the function in a recurring pattern.


To find the factorial of a numebr generally we would write:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
	int a,num,fact=1;
	printf("Enter the number to find factorial");
	scanf("%d",&num);
	for(a=1;a<=num;a++)
	{
		fact*=a;
	}
	printf("Factorial= %d",fact);
}
Now with recursion this would become ::

Code:
int factorial(int n)
{
   if(n == 1)
   {
      return 1;
   }
   else
   {
      return n * factorial(n - 1);
   }
}
void main()
{
	int num,fact;
	printf("Enter the number for factrial");
	scanf("%d",&num);
	fact=factorial(num);
	printf("Factorial= %d",fact);
}

This image shows the implementation of the factorial program using recursion.

As long as n is greater than zero, this function will run recursively; ie, it calls itself with the value of n decreased by 1.The stopping point, is called the base case. A base case is the bottom point of a recursive program, generally the termination and the answer returning point.

All recursive programs have at least one base case which is definite to be attained; otherwise the program would run infinitely. When a function calls itself, a new copy of that function is runin which the local variables in the scope of the second version are independent of the local variables in the first, and they cannot affect one another directly even being parts of the same functions in the same call but in different runs.

Recursion finds great use in stacks and linked lists.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help drawing Sierpinski triangles using recursion dalearyous Java Help 3 09-18-2007 12:55 PM
Recursion Problem cooldude C and C++ 3 11-29-2006 11:42 AM


All times are GMT -5. The time now is 10:41 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads