title: Recursion and the Return Keyword
body: <p>I'm currently working my way through the Java tutorials and am currently on Recursions.</p>
  
  <p>I have the following code which calculates the factorial of any number passed to the factorial method</p>
  
  <pre><code>public class App {
      public static void main(String[] args) {
          //E.g 4! = 4*3*2*1(factorial 4)
          System.out.println(factorial(4));
  
      }
      private static int factorial(int value){
          //System.out.println(value);
          if (value == 1){
              return 1;
          }
          return factorial(value - 1)*value;
      }
  }
  </code></pre>
  
  <p>I have trouble understanding the part</p>
  
  <pre><code>if (value == 1){
      return 1;
  }
  return factorial(value - 1)*value;
  </code></pre>
  
  <p>My understanding is that the return keyword simply terminates the method, and/or returns a value of the same type declared by the method(i.e int,String, etc).</p>
  
  <p>What happens when the  following line is run?</p>
  
  <pre><code>return factorial(value - 1)*value;
  </code></pre>
  
  <p>the function is returning the total of (value - 1) * value, which would give me</p>
  
  <pre><code>(3)*4 = 12
  (2)*3 = 6
  (1)*2 = 2
  </code></pre>
  
  <p>with each passing iteration.However, <code>System.out.println(factorial(4));</code> gives me a total of <code>24</code>. How is this number derived from the method? There are no variables to store the sum of the values, so where is the program storing them? Also, how do I get <code>24</code> from these values?</p>
  
  <pre><code>(3)*4
  (2)*3
  (1)*2
  </code></pre>
  
  <p>While I know that <code>24</code> is derived from <code>4*3*2*1</code>, I do not see how that can be calculated from the above.</p>
  
  <p>Any explanation would be greatly appreciated.</p>