简体   繁体   中英

What happens when a return statement where method calls itself? Recursion.

So I made this simple code with some help of a tutorial. Going through it i am not quite sure what happens at return faktor(tall-1)*tall .

in the return statement the method calls itself and returns a value. What happens first? Does it call itself first, or does it first return the values, or do they both happen at the same time?

When it returns the values what happens then?

package enkelrekursjon1;

public class Enkelrekursjon1 {

    public static void main(String[] args) {

       System.out.print(faktor(3)); 
    }

    private static int faktor(int tall){
    System.out.print(tall); 
    if (tall != 1){
    return faktor(tall - 1)*tall; 
    }    
    return 1; 
    }

}

It works the same way a return statement returning a value always works:

  • The expression is evaluated (in this case faktor(tall - 1) * tall )
  • The value is returned to the caller

It can't possibly return before evaluating the expression, because there'd be no value to return.

So in your case, if you have call faktor(5) you'll end up with a call stack of:

faktor(5)
   -> calls faktor(4)
         -> calls faktor(3)
               -> calls faktor(2)
                     -> calls faktor(1)
                           -> returns 1
                     -> returns 1 * 2 (i.e. 2)
               -> returns 2 * 3 (i.e. 6)
         -> returns 6 * 4 (i.e. 24)
   -> returns 24 * 5 (i.e. 120)

I have wondered about it myself and the best way to know it is by letting the program find it out, so I modified your code to see what actually happens. Here is the code that I modified (I commented and written it in your native tongue and I think we live in the same country. Er ikke norsk dog. lol.)

public class EnkelRekursjon1 {

    private static String funksjonNavn= "";
    private static int resultat=0;
    public static void main(String[] args) {

        int tall = 3; 
       System.out.print(tall+"! = "+faktor(3)); // resultat printes etter rekursjon 
    }

    private static int faktor(int tall){
    funksjonNavn="Faktor("+tall+")"; 
    System.out.println(tall);
    //finn hvilken funksjon vi er. 
    System.out.println("Funksjonen kaller "+ funksjonNavn);
    if (tall != 1){
        resultat= faktor(tall - 1)*tall; 
        System.out.println("Reusltatet er "+ resultat);
        return resultat; 
    }
    System.out.println("-------------------------------");
    //finn resultat for hver "rekursjon"
    System.out.println("Reusltatet er "+ (resultat+1));
    //blir 1* 0 her tror jeg, så funksjonen returnerer 1 istedenfor 0   
        return 1; 
    }

}

and here is the result:

在此处输入图片说明

If you want to evaluate it manually, it will look like this .

faktor(5) = faktor(4)*5

we'll get the value of faktor(4), and it's like this:

faktor(4) = faktor(3)*4

we'll do the same to the rest of the "faktor(int tall)" until the condition is false.

faktor(3) = faktor(2)*3 
faktor(2) = faktor(1)*2

faktor(1) will return 1 since tall is equal to 1 (hence, the condition is false). substituting faktor(1) with 1,

we get:

faktor(2) = 1*2 =2

substitute faktor(2) with 2, we get

faktor(3)= 2 * 3 = 6

substituting faktor(3) with 6, we get

faktor(4)= 6*4 = 24

finally, we get:

faktor(5) = 24*5 = 120

I hope it's clear enough this time

I hope I have answered your question. Please don't hesitate to ask if you have further questions. Ha ein fin dag :)

This is a recursive structure. It uses the condition

if(tall != 1)

as the base case. So this method will go through and do some work, check if tall is equal to one, if not it will go through and operate on the data again. So on and so forth until the base case is satisfied (tall==1)

Hope this helps

First, if the condition is true, the method calls itself. The method will only return when tall reaches 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM