title: Counting recursive calls of a function
body: <p>I have recursive code for generating the Catalan numbers.</p>
  <p>I managed to write the recursive call, but for some reason, the counter is not working properly.</p>
  <p>For example, the number of calls for the 7th Catalan number should be 1215.</p>
  <p>The return value needs to be a tuple of the Catalan number and the number of calls, for example: (429,1215).</p>
  <p>Original code:</p>
  <pre><code>def catalan_rec(n):
      if n&lt;=1:
          return 1
      res=0
      for i in range(n):
          res+=catalan_rec(i)*catalan_rec(n-i-1)
      return res
  </code></pre>
  <p>Counter code:</p>
  <pre><code>def catalan_rec_count(n,counter=1):
      if n&lt;=1:
          return 1
      res=0
      for i in range(n):
          res+=catalan_rec_count(i,counter+1)*catalan_rec_count(n-i-1,counter+1)        
      return (res,counter)
  </code></pre>