C program recursive function.

Thread Starter

ecjohnny

Joined Jul 16, 2005
142
Can anyone help/explain me with this C program recursive function. These 2 function are recursive functions. They both compute (an).
1) Do they have any differences? e.g which is better and why and so on? what they other does compared to another.
2) what are the limitations for this 2 functions?




Function 1:
int f1 (int a, int n)
{
if (n == 1)
return a;
m = n/2;
return f1(a, m) * f1(a, n-m);
}


Function 2:
int f2 (int b, int x)
{
if (x == 1)
return b;
y = x/2;
temp = f2(b, y);
temp *= temp;
if (x%2 == 0)
return temp;
else
return temp*b;
}
 
There isnt a lot of magic in recursive functions, all they are doing is workling on a stream of data until the final event occurrs.

In 35 years of programming I have never used one in anger, I get away with normal simple loops working on data arrays.
 

ErnieM

Joined Apr 24, 2011
8,040
I actually wrote one (ONE) recursive function that will probable never recurse. It's an assembler macro to make an inline wait, and for very long waits it just calls itself again.
 
Top