Write a few paragraphs that define recursion to someone who does not know what recursion is. Include an example of Scheme code in which recursion helped make the function more readable.
In program recursion is function called by it self directly or indirectly and the function which include recursion call it is called recursive function. Recursion is used in many data structure algorithms like Inorder/Preorder/Postorder traversals of tree, DFS of graphs, towers of hanoi problem etc. Recursion makes program more efficient. Recursion is a solution of problems which are created in iterative program.
Base condition is very important in recursive function.
what is base case?
Base case is stopping criteria/Stopping condition for recursive function.It is indicate at which condition recursive function call is over.If base case is not defined or not reached then stack overflow problem may arise.
In internal execution of recursive function stack will be used.
Advantages of recursive programming:
1.Recursive provides a simple and understandable way to write a code.
2.It reduce unnecessary function calling.
Disadvantages of recursive programming:
1.It very difficult to trace and it is always logical.
2.It use more processor time.
3. Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC.
Example:
write a c program to find the factorial of given positive number.
#include<stdio.h>
int fact(int n)
{
if(n==0) //Base case
return 1;
else
return (n*fact(n-1)); //recursive function call
}
void main()
{
int x,ans;
printf("enter value of x:");
scanf("%d",&x);
ans=fact(x); //main function call
printf("factorial of %d is %d",x,ans);
}
Write a few paragraphs that define recursion to someone who does not know what recursion is....