Compare Iterative and Recursive Functions?
https://www.computersprofessor.com/2016/06/compare-iterative-and-recursive.html
|
ITERATIVE FUNCITONS
|
|
RECURSIVE FUNCTIONS
|
A)
|
The process is repeated
until a condition is satisfied
|
A)
|
The function repeatedly calls itself till the
terminating condition is satisfied.
|
B)
|
The sts included in the
loo are executed repeatedly
|
B)
|
The intermediate results
are stored on to the stalk each time the function recursively called
|
C)
|
Each time the loop is
executed, the intermediate results are refined
|
C)
|
The final value will be
evaluated often retrieving the values stored on the stack
|
|
Ex: int gcd ( int a, int
b)
{
int r;
r = a%b;
while ( r!=0)
{
a=b;
b=r;
r=a%b;
}
return b;
}
|
|
Ex: int gcd ( int a, int
b)
{
int r;
r =a%b;
if ( r==0)
return b;
else
gcd ( b,r);
}
|