How recursion occurs in the following code

Asked

Viewed 2,262 times

2

I would like to understand, because I can’t see mentally how it occurs in the excerpt: Return fat ː factorial.factoring(fat-1);, the issue of recursion, in what concerns the calls of the method itself factoring in relation to multiplication with fat.

public class FatorialRecursiva
{   
   public static void main(String[] args)
   {
       FatorialRecursiva fatorial = new FatorialRecursiva();
       double dado = 5.0;
       dado = fatorial.fatorar(dado);

       System.out.print(dado);
   }

   public double fatorar(double fat)
   {
       if (fat < 2)
       {
           return 1;
       }else
       {
           return fat *  fatorar(fat-1);
       }
   }
}
  • 1

    He creates another instance to solve the "subproblema", which you see creates another, and so it goes .... The definition of recursion is not necessarily, "a method calling itself"

  • 1

    these instances make no sense... recursiveness will happen, but in an unsanctioned way..

  • Daniel Omine, as I said in another report, had not realized that he had instantiated the class both in the main method and in the factoring, corrected and I hope that now is correct.

  • 1

    @Brunosilva If you want to better understand how the step-by-step execution of a function/method works recursivo here an example: http://answall.com/questions/111520/d%C3%Bavida-com-recursividade *Obs - Great care should be taken with recursive functions/methods, as they can be entered into infinite loop easily!

2 answers

9


Here’s a gif that helps you understand quietly:

http://www.cienciacomputacao.com.br

  • 1

    gif presented just what I was looking for in response. Thank you all for your contributions.

  • 1

    A gif is worth a thousand words, congratulations, great response. + 1

  • Excelent Gif. If you are more curious about the subject, I also suggest that you study the code evaluation procedures. I had difficulty visualizing this and understanding better how code is evaluated 'changed my life'. I suggest this chapter of a classic book: The Elements of Programming of the book Structure and Interpretation of Computer Programs. This and the next chapter give a great view on the theme.

0

Basically what happens is that a method performs itself. Since the programming is object oriented, the execution of the code is not all linear and makes it possible to execute a single piece of code several times.

I recommend reading the article in which I removed the excerpt below, which uses as an example the code you used in the question:

"Recursion works similarly to a repetition loop, in fact everything we do in a loop can be done in recursion. Recursion is nothing less than a function within the other and it must be thought of as a stack."

  • 1

    I think that’s not quite it, I believe the doubt lies precisely in the fact that a method does not execute itself, and yet, it is a recursive solution. Each instance method is a new object, of the same class, and delegates to the method of that object the task of solving the subproblem. An "this.factorial" instead of "factorial.factorial" would be a method running itself, and in my view, would still be an OO code.

  • 1

    The method executes itself when in the second Return: return fat * fatorial.fatorar(fat-1);. I believe he still doesn’t understand well how recursivity works and is just trying to understand this with the question, especially when he said "I can’t glimpse mentally".

  • 1

    Now I understand your ready of recursion, but I do not agree. For me the same method of class instance different are equal, you understand?

  • 1

    No, factorial receives a new instance: "Factorialrecursiva fatorial = new Factorialrecursiva();", ie technically, is another method. From what I understood of the question, he cannot mentally visualize how the call of a method occurs, within the method itself, in the line he quoted. The answer is simple. There is no such call, it is another method, another object. However, at least for me, this is still a recursive solution to a problem. Implemented in a less efficient way.

  • 1

    Different instantiated objects of the same class have different methods. I don’t think it’s right to claim that they’re the same, just because in this case, they’re going to have the same behavior.

  • Well the way I posted the code may be bringing a little confusion however now I realized that I instated the class both in the main method and in the factoring method, but I see this as incorrect, I should have instantiated only in the main method, I will correct. And in case my doubt is in really how does recursiveness occur.

Show 1 more comment

Browser other questions tagged

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