# Thread: Recursive function

1. ## Recursive function

How does the fRecur function works and what value does it return to main?

Somebody help!!

#include "stdio.h"

enum con{MIN_NUM=0, MAX_NUM=100};

int fRecur(int n);

main(void)
{
int i,suma=0;

for(i=0;i<=MAX_NUM;i++) suma+=i;
printf("El valor de suma es %i\n",suma);
printf("El valor de suma que devuelve fRecur es %i\n",fRecur(MAX_NUM));

return(0);
}

fRecur(int n)
{
if(n==MIN_NUM) return 0;
else return fRecur(n-1)+n;
}

2. No cierto, pero - por favor, aqui nos hacemos los Ingles!!
Buena suerte!

3. Actually, from the looks of it, the function will never stop since the number just keeps growing...I think you have waht we call here a "bum algorithm".

4. <fx: runs code>
El valor de suma es 5050
El valor de suma que devuelve fRecur es 5050

Now what was the question?

> How does the fRecur function works
Imagine a call to fRecur(2)

This will result in
return fRecur(2-1)+2;

So now do the same, (2-1) results in fRecur(1)
return fRecur(1-1)+1;

So now do the same, (1-1) results in fRecur(0)
Aha, this is different,
if(n==MIN_NUM) return 0; /* end of the road */

Working your way back fRecur(0) returns 0, so
return fRecur(1-1)+1;
is
return 0+1;

And
return fRecur(2-1)+2;
is
return 1+2;

Popular pages Recent additions