0

I am in a java class at my highschool so I am very new to writing code.

For a recursive method, how does using the word return effect the output? I assume return means and end to the program.

For this program, what would random(15) output? Would it just keep looping until inevitably c was 10 and then return 80? If possible, please break this down step by step as I am having trouble understanding it.

public int random(int c) 
{
    if( int c > 10)
        return random(c - 1);
    return c * 8;
}

How does this code differ from this code, that does not have the return keyword.

public int random(int c) 
{
    if( int c > 10)
        random(c - 1);
    return c * 8;
}
Sam
  • 35
  • 5
  • 3
    "_this code ... that does not have the return keyword._" It **does** have the return keyword, twice. In fact, I can't see any difference between the 2 code samples. "_For this program, what would random(15) output?_" Just run the code and see. – takendarkk Feb 21 '18 at 21:31
  • 1
    Both your code samples are the same. Please correct it. – TM00 Feb 21 '18 at 21:45
  • 3
    "_please break this down step by step as I am having trouble understanding it_" Look up a tutorial on how to use a debugger as that is exactly what it allows you to do - step through your code one line at a time. – takendarkk Feb 21 '18 at 21:47
  • like @csmckelvey mentioned, just run it and see what the result is. If the result alone doesn't help try stepping through with a debugger and be sure to use the step into option when you encounter the line `return random(c - 1)` – JustWannaFly Feb 21 '18 at 21:48

2 Answers2

1

First of all I'm not sure why you need to ask this question, in fact I think you understand the concept of recursive methods quite good.

First Snippet

As you explained correctly random(15) returns a value of 80.

public static void main(String[] args) {
    System.out.println("Result: " + random(15));
}

private static int random(int c) {
    if (c > 10) {
        System.out.println("c is greater than 10");
        return random(c - 1);
    }
    System.out.println("multiplying c=" + c + " by 8");
    return c * 8;
}

Output:

run:
c is greater than 10
c is greater than 10
c is greater than 10
c is greater than 10
c is greater than 10

multiplying c=10 by 8

Result: 80
BUILD SUCCESSFUL (total time: 0 seconds)

Just for explanation, the variable c is decreased by 1 five times and then finally multiplied by 8.

Second Snippet

I just assume that your second method should look something like this:

public static void main(String[] args) {
    System.out.println("Result: " + random(15));
}

private static int random(int c) {
    if (c > 10) {
        System.out.println("c is greater than 10");
        random(c - 1);
    }
    System.out.println("multiplying c=" + c + " by 8");
    return c * 8;
}

This time, the output looks different and also the result is different.

Output:

run:
c is greater than 10 // method a
c is greater than 10 // b
c is greater than 10 // c
c is greater than 10 // d
c is greater than 10 // e

multiplying c=10 by 8 // --> random(c - 1); in method e
multiplying c=11 by 8 // happening in method e
multiplying c=12 by 8 // d
multiplying c=13 by 8 // c
multiplying c=14 by 8 // b
multiplying c=15 by 8 // a

Result: 120
BUILD SUCCESSFUL (total time: 0 seconds)

You can see that your variable c is decreased by 1 in each method (a - e) and then equal to 10-15. At the end only the last multiplication matters, which is 15 * 8 of course, and the result of this operation is then displayed as the result.

Cobra_8

Cobra_8
  • 199
  • 1
  • 9
0

Even though you should probably do some research about recursive methods, I'll try to explain the difference as simple as possible.

In your first method, if c > 10 then the method returns the result of the call to itself with the parameter c - 1, this means that as long as c is greater than 10, the method will get called and c will decrease by one until it's equal to 10 to then return 80 (10 * 8).

The second method does nothing special, in fact you can say that it's like only returning c * 8, why? Because you're calling the same method with c - 1 but you're not using the result and the code gets out of the if statement and goes to the return c * 8; so no matter how much is c, the result will always be c * 8.

On the other hand with the first method, it'll get called recursively until c reachs 10 and will then return 80 back to the very first call of the method (see https://introcs.cs.princeton.edu/java/23recursion/ for more informations).

Haytam
  • 4,643
  • 2
  • 20
  • 43