If you have this loop:
-
for(int i=0; i <10; ++1)
-
{
-
printf("Hello\n");
-
}
-
Then you can write this recursive function:
-
void MyFunction(int arg)
-
{
-
if (arg <10)
-
{
-
printf("Hello\n");
-
MyFunction(++arg);
-
}
-
return;
-
}
-
main()
-
{
-
MyFunction(0);
-
}
-
Do you see how the loop appears inthe recursive function?
Yep, but you are showing a 'primitive recursive' function here; even a simpler
'tail recursive' function. If you want to try it with a 'total recursive' function you
at least need an explicit stack so a recursive solution is as good and even more
efficient as an iterative solution. Try this one (the Ackermann function):
-
int A(int m, int n) {
-
if (!m) return n+1;
-
if (!n) return A(m-1, n);
-
return A(m-1, A(m, n-1));
-
}
-
kind regards,
Jos