What is a recursive method?

1

Is the resultado variable within for recursive?

#include <stdio.h>

int main(void) {
int N,i;
double resultado=0.0;

scanf("%d",&N);

for (i=0; i<N; i++)
  {
   resultado= 1.0 / (resultado+2); 
  }

  printf("%0.10lf\n",1.0+resultado);

return 0;
}
    
asked by anonymous 21.08.2018 / 20:16

1 answer

5

In C we have functions, not methods. Variables are not recursive because to have recursion needs an action and variable is state.

In general it can be used in place of a loop of repetition. Most of the time it should not. Leave the recursion for cases where it is more intuitive. Normal sequences work best in loops. It is possible to transform this loop into a recursive function, but do not do it without a reason to do so.

Take care to do a recursive function, that is, a function that calls itself, unconditionally. When she does this she goes to infinity and never goes back to the beginning. And if you've been in it, this site will happen here, or the stack overflow , which will break the memory exhaustion request that is being filled in the stack without being released at any time.

Recursion is the repetition of something. This is recursion:

Inprogrammingistocallaroutineinsideitself.

Youhavesomequestionsonthesiteaboutthetopic:

To understand recursion you need to understand recursion! Got it?

Anothertypicalexample:

    
21.08.2018 / 20:21