title: Python function: return an array without additional memory allocations
body: <p>Suppose I want to make a function that multiplies input vector by input matrix:</p>
  
  <pre><code>def MatMul(A,b):
      return A.dot(b)
  </code></pre>
  
  <p>Now, I execute the following code:</p>
  
  <pre><code>import numpy as np
  A=np.array([[1,2,3],[4,5,6],[7,8,9]],dtype='float64')
  b=np.array([4,5,6],dtype='float64')
  c=np.zeros(3,dtype='float64')
  c=MatMul(A,b)
  </code></pre>
  
  <p>Will there be additional array allocation inside <code>MatMul</code> function? I know that <code>A</code> and <code>b</code> will be passed by reference. Note, that I've already preallocated array <code>c</code>. </p>
  
  <p>Generally, how to avoid unnecessary preallocations in simple functions like this? Say, I want to perform several mathematical operations:</p>
  
  <pre><code>def Rank1Update(A,b,alpha):
      c=A.dot(b)
      c+=alpha*c.dot(c)*c
      return c
  </code></pre>
  
  <p>I can fit many mathematical functions in 1 line, but code quickly becomes unreadable.</p>
  
  <p>I am familiar with C-style programming, where in order to avoid unnecessary memory allocations one would pass <code>A</code>,<code>b</code> and <code>c</code> by references and update <code>c</code> inside the functions that returns <code>void</code>. I can do the same in python, but I want to use <code>return</code> for my convenience and code readability,</p>
  
  <p>Thanks,</p>
  
  <p>Mikhail </p>