简体   繁体   English

递归调用的返回值是多少

[英]What is the return value of a recursive call

java version "1.8.0_92"

I have the following code I am trying to understand and trace. 我有以下代码正在尝试理解和跟踪。

The part I don't understand is the * a at the end. 我不明白的部分是* a结尾。 When does that multiplication get called? 乘法何时被调用? And what is the return value of power1(a, n - 1); power1(a, n - 1);的返回值是多少power1(a, n - 1);

Is it n - 1 * a n - 1 * a

 public static double power1(double a, int n) {
        double result;
        if(n == 0) {
            return 1;
        }
        else {
            result = power1(a, n - 1) * a;

            return result;
        }
    }

You can modify the code to print a trace of the recursion. 您可以修改代码以打印递归的痕迹。 That can help you understand what is going on. 那可以帮助您了解发生了什么。

/**
 * Print string form of `o`, but indented with n spaces
 */
private static void printIndented(int n, Object o) {
    while (n-->0) System.out.print("  ");
    System.out.println(o);
}

/* 
 * Added a third param `d` to keep track of the depth of the recursion
 */
public static double power1(double a, int n, int d) {
    // Entering a "possible" recursive call
    printIndented(d, "call power1, a=" + a + ", n=" + n + ", d=" + d);

    double result;
    if(n == 0) {
        // Returning from the base case, this should have the largest depth.
        printIndented(d, "return 1.0");

        return 1;
    }
    else {
        result = power1(a, n - 1, d + 1);

        // Return from intermediate recursive calls, we print
        // the value of power1(a, n-1) as well.
        printIndented(d, "return " + result + " * " + a);
        return result * a;
    }
}

public static void main(String [] args) {
    System.out.println(power1(1.4, 3, 0));
}

Output 输出量

call power1, a=1.4, n=3, d=0
  call power1, a=1.4, n=2, d=1
    call power1, a=1.4, n=1, d=2
      call power1, a=1.4, n=0, d=3
      return 1.0
    return 1.0 * 1.4
  return 1.4 * 1.4
return 1.9599999999999997 * 1.4
2.7439999999999993

As you can see, the value from the inner return becomes the result in the outer return statement. 如您所见,内部return的值成为外部return语句的result

Return value of power1(a,n-1) power1(a,n-1)的返回值

We can see that power1 is defined as public static _double_ power1(double a, int n) This means that on the line 我们可以看到public static _double_ power1(double a, int n)被定义为public static _double_ power1(double a, int n)这意味着

result = power1(a, n - 1) * a;

the type will be : 类型将是:

double = double * double;

When the multiplication gets called 当乘法被调用时

We start our function with n and a . 我们从na开始我们的功能。 a will be constant in this implementation. 在此实现中, a将保持不变。 It's given straight as to the next recursive call. 直接给出下一个递归调用。

Yet n varies. 但是n有所不同。

We call power1(a,n) . 我们称power1(a,n) It checks first if n == 0; 它首先检查n == 0。 It is not. 它不是。 So we go to the else part of the if. 因此,我们转到if的else部分。 Time to calculate result . 计算result时间。

To know what the value of result is, we need power1(a,n-1). 要知道result的值是什么,我们需要power1(a,n-1)。 We proceed. 我们继续。 n-1 can be 0 or can not be. n-1可以为0或不能为0。

If it's 0, we return 1, and we now know that power1(a,n-1) = 1. We can now multiply it with a. 如果为0,则返回1,现在知道power1(a,n-1)=1。我们现在可以将其乘以a。

If it's not 0, then we need a new result, seeing as we're in a completely seperate call of the power1 method. 如果它不为0,那么我们需要一个新的结果,因为我们正在完全分开调用power1方法。 We now need power1(a,n-2). 现在我们需要power1(a,n-2)。 We check again for 0. If n-2 == 0, return 1 to get the caller to calculate power(a,n-1). 我们再次检查0。如果n-2 == 0,则返回1以使调用方计算出power(a,n-1)。 If not go down another call, now with n-3... Etc 如果不接听另一个电话,现在使用n-3 ...等

In terms of the timing of calling the multiplication. 在调用乘法的时间方面。

It's gonna call all the (n) recursive calls first, before doing n multiplications. 在进行n次乘法之前,它将首先调用所有(n)个递归调用。

If you call power1 with a = 2 and n = 3 , this is what will happen: 如果使用a = 2n = 3调用power1,则会发生以下情况:

result = power1(2, 2) * 2;
power1(2, 2) = power1(2, 1) * 2;
power1(2, 1) = power1(2, 0) * 2;
power1(2, 0) = 1

In the above example, the * a happens three times in total. 在上面的示例中, * a总共发生3次。 It is called whenever power1(a, n) returns a value. 只要power1(a,n)返回一个值,就会调用它。 The first power1(a, n) to return a value will be power1(2, 0) (this is because n = 0 is your "base" case). 返回值的第一个power1(a, n)将是power1(2, 0) (这是因为n = 0是您的“基本”情况)。 Then power1(2, 1) will return 2. Then power1(2, 2) will return 4. Then your initial call to power1(2, 3) will return 8. 然后power1(2, 1)将返回2。然后power1(2, 2)将返回4。然后您对power1(2, 3)初始调用将返回8。

First you call power (a,n-1) until n == 0 which is your base case . 首先,将幂(a,n-1)称为n == 0,这是您的基本情况。

Then the 1 gets returned and is multiplied by the value of a. 然后返回1并乘以a的值。 Then a is returned to the previous call where it is multiplied by a . 然后,将a返回到上一个调用,在此将其乘以a。 The process is repeated N times and thus you get A^n. 该过程重复N次,因此得到A ^ n。 I would recommend you go throught one example of this code giving it some initials values and tracing the code using pen and paper. 我建议您仔细阅读该代码的一个示例,为它提供一些初始值,并使用笔和纸来跟踪代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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