Recursion is the process in which the function calls itself repeatedly until the bounds of the connection is satisfied.
A function is said to be recursive if a statement in the body of the function calls itself.In recursive function we must specify the terminating conditions; otherwise it will go on infinitely.
Recursive function are more beneficial in the data structures like linked list,double linked list,and trees.
Void main()
{
int x,y;
--------------
sum(x);
--------------
}
sum(int a)
{
----------------
sum(a-1);
--------------
}
Here you can see the sum() function is called in the body of sum() function.
If we call the function recursively, the condition for calling this function is known as recursive condition.Its the recursive condition which calls the function again and again and the condition which will terminate this looping structure is known as terminating condition.Always remember a terminating condition should come before the recursive condition, like:
sum(int x)
{
------------
if(x = = 1) // terminating condition
return (1);
Else
p=(sum x-1); // recursive condition
-------------
}
Here the if statement is providing terminating condition and the else part is having recursive condition.if the recursive condition comes before the terminating condition,then the function will go into infinite loop.
A function is said to be recursive if a statement in the body of the function calls itself.In recursive function we must specify the terminating conditions; otherwise it will go on infinitely.
Recursive function are more beneficial in the data structures like linked list,double linked list,and trees.
Void main()
{
int x,y;
--------------
sum(x);
--------------
}
sum(int a)
{
----------------
sum(a-1);
--------------
}
Here you can see the sum() function is called in the body of sum() function.
If we call the function recursively, the condition for calling this function is known as recursive condition.Its the recursive condition which calls the function again and again and the condition which will terminate this looping structure is known as terminating condition.Always remember a terminating condition should come before the recursive condition, like:
sum(int x)
{
------------
if(x = = 1) // terminating condition
return (1);
Else
p=(sum x-1); // recursive condition
-------------
}
Here the if statement is providing terminating condition and the else part is having recursive condition.if the recursive condition comes before the terminating condition,then the function will go into infinite loop.
0 comments:
Post a Comment