title: Understanding the scala substitution model through the use of sumInts method
body: <p>I'm doing a scala course and one of the examples given is the sumInts function which is defined like : </p>
  
  <pre><code>  def sumInts(a: Int, b: Int) : Int = 
      if(a &gt; b) 0  
      else a + sumInts(a + 1 , b)
  </code></pre>
  
  <p>I've tried to understand this function better by outputting some values as its being iterated upon : </p>
  
  <pre><code>class SumInts {
        def sumInts(a: Int, b: Int) : Int = 
          if(a &gt; b) 0 else 
          {     
            println(a + " + sumInts("+(a + 1)+" , "+b+")")       
            val res1 = sumInts(a + 1 , b)
            val res2 = a
            val res3 = res1 + res2
            println("res1 is : "+res1+", res2 is "+res2+", res3 is "+res3)
            res3
          }
  }
  </code></pre>
  
  <p>So the code : </p>
  
  <pre><code>object SumIntsMain {
  
      def main(args: Array[String]) {
  
        println(new SumInts().sumInts(3 , 6));
  
    }
  
  }
  </code></pre>
  
  <p>Returns the output : </p>
  
  <pre><code>3 + sumInts(4 , 6)
  4 + sumInts(5 , 6)
  5 + sumInts(6 , 6)
  6 + sumInts(7 , 6)
  res1 is : 0, res2 is 6, res3 is 6
  res1 is : 6, res2 is 5, res3 is 11
  res1 is : 11, res2 is 4, res3 is 15
  res1 is : 15, res2 is 3, res3 is 18
  18
  </code></pre>
  
  <p>Can someone explain how these values are computed. I've tried by outputting all of the created variables but still im confused.</p>