# recursive function

This is a discussion on recursive function within the C Programming forums, part of the General Programming Boards category; how can we convert an iterative function into recursive function .. i know few recursive functions but every time i ...

1. ## recursive function

how can we convert an iterative function into recursive function ..

i know few recursive functions but every time i am asked to convert an iterative function into a recursive one ...i just get perplexed...

how do we really go for a recursive function ...i mean is there any particular way that i can try cracking an iterative function into its corresponding recursive version..

2. You don't convert iterative functions to recusive functions. When you think what is a recursive function, think also what kind of written code it could be used to replace. Recursive conversion, is possible on loops (while, for, do - while, if goto, etc).
Code:
void countdown(int start)
{
int n = start;
while(n>0)
{
printf("Countdown to &#37;d\n", n);
n--;
}
}
The example function presented can be converted to a recursive.
Code:
void countdown(int n)
{
if(n==0)
return;  /* Break recursion */

printf("Countdown to %d\n", n);
countdown(n-1);
}
Of course this is a simple example but the same principle applies to most recursive transformations.
It might interest you to know, that the task of converting recursive to non-recursive functions, is actually of much more use, and interest.