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