What is a recursive method?

Asked

Viewed 759 times

2

The variable resultado within the for is 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;
}

1 answer

7


In C we have functions and 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 repeat loop. Most of the time it should not. Leave the recursion to cases where it is more intuitive. Normal sequences work best in loops. It is possible to transform this loop into recursive function, but do not do so without a reason to do so.

Take care to make a recursive function, that is, a function that calls itself, unconditionally. When you do this it goes to infinity and never goes back to the beginning. And if you have been there will occur this site here, or the stack overflow, what will break the application by memory exhaustion that is being occupied in the pile without being released at any time.

Recursion is the repetition of something. This is recursion:

Triângulos formados por triângulos

In programming is calling a routine within itself.

There are some questions on the site about the theme:

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

Livro com índice na página de recursão indica para a própria página

One more typical example:

Recursão em vídeo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.