title: Difficulty in understanding a recursive decorated function
body: <p>I have the below code where I am decorating a recursive function, but i am unable to understand some portions of the code.</p>
  
  <pre><code>def deco_func(f):
      def wrapper(*args):
          print('Decorating', args)
          res = f(*args)
          print(res)
  
      return wrapper
  
  
  @deco_func
  def fact(n):
      if n == 0:
          return 1
      else:
          return n * fact(n - 1)
  
  fact(4)
  </code></pre>
  
  <p><strong>Note :</strong></p>
  
  <p><strong><em>1. f inside the wrapper function actually points to original version of fact function since it comes from enclosing scope and its value is closed by wrapper.</em></strong></p>
  
  <p><strong><em>2. Recursive calls inside the fact function calls the decorated version of fact which is wrapper.</em></strong></p>
  
  <p>So as per my understanding the stack trace would look like this.</p>
  
  <pre><code>res = f(4)
      = 4 * wrapper(3) = 4 * f(3)
      = 4 * 3 * wrapper(2) = 4 * 3 * f(2)
      = 4 * 3 * 2 * wrapper(1) = 4 * 3 * 2 * f(1)
      = 4 * 3 * 2 * 1 * wrapper(0) = 4 * 3 * 2 * 1 * f(0)
  </code></pre>
  
  <p>At this point n will be 0 and hence f(0) will return 1 so that stack would look like below</p>
  
  <p><code>res    = 4 * 3 * 2 * 1 * 1</code></p>
  
  <p>So <code>res</code> should be 24, but its value is 1 which i am not able to understand. Also at this point i get an exception </p>
  
  <p><code>TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'</code></p>
  
  <p>But as soon as I add a return statement at the bottom of the wrapper function like <code>return res</code>, everything works fine. </p>
  
  <p>I do not understand why not having a return is creating a problem here, because inside the wrapper function control never goes beyond <code>res = f(*args)</code> till recursion is complete. So a final call like <code>f(0)</code> would result in <code>res = 5*4*3*2*1</code> and as above illustrated res should be evaluated to 24 and then  the control should go to next statement. So why NOT having the return statement in wrapper is creating problem when the return would only execute once <code>res</code> has been calculated</p>
  
  <p>I know i am missing something major here but i am not able to figure it out conceptually what goes wrong if we remove the return statement.</p>