title: A couple of questions on recursive functions in C language
body: <p>This is a function to get sum of the digits of a number:</p>
  
  <pre><code>int sumOfDigits(int n)
  {
      int sum=0; //line 1
      if(n==0)
          return sum;
      else
      {
          sum=(n%10)+sumOfDigits(n/10); //line 2
          // return sum;  //line 3
      }
  }
  </code></pre>
  
  <p>While writing this code, I realized the scope of the local variables is local to each individual recursion of the function. So am I right in saying that if <code>n=11111</code>, 5 <code>sum</code> variables are created and pushed on the stack with each recursion? If this is correct then what is the benefit of using recursion when I can do it in normal function using loops, thus overwriting only one memory location? If I use pointers, recursion will probably take similar memory as a normal function.</p>
  
  <p>Now my second question, even though this function gives me the correct result each time, I don't see how the recursions (other than the last one which returns 0) return values without uncommenting line 3. (using geany with gcc)</p>
  
  <p><strong>I'm new to programming, so please pardon any mistakes</strong></p>