title: Multiple calls of a function inside other functions
body: <p>I have the following functions:</p>
  <pre><code>def f(x):
      return x
  
  def f2(x):
      y = f(x)
      return y + x + 1
  
  def f3(x):
      y = f(x)
      return y + x + 2
  
  def DoAll(x):
      i2 = f2(x)
      i3 = f3(x)
      return i2, i3
  
  print(DolAll(2))
  </code></pre>
  <p>Even when this code runs, it seems very inefficient since I call <code>f(x)</code> multiple times. How can I solve this problem without defining an <code>f2(x, y)</code> and <code>f3(x, y)</code>? I would like to use something similar to</p>
  <pre><code>def f(x):
      return x
  
  def f2(x):
      nonlocal y
      return y + x + 1
  
  def f3(x):
      nonlocal y
      return y + x + 2
  
  def DoAll(x):
      y = f(x)
      f2 = f2(x)
      f3 = f3(x)
      return f2, f3
  
  print(DolAll(2))
  </code></pre>
  <p>Of course, the code shown here does not work.</p>