title: Python Passing variables within (sub) functions
body: <p>I am trying to understand a basic concept about passing variables and scoping in python but I am clearly missing something as I have spent a lot time reading but not getting what I am expecting.</p>
  
  <p>I expect the following as:</p>
  
  <ol>
  <li><p>Define global a in main ()</p></li>
  <li><p>In define_stuff(), add a to 1 and assign to b, pass b or 3  back to main().</p></li>
  <li><p>In add_stuff(), add a to b and assign c, pass c or 5 back to main().</p>
  
  <p>def main():
      a = '2'</p>
  
  <pre><code>def define_stuff():
      b = 1 + float(a)
      print 'returning', b
      return b
  
  def add_stuff():
      print 'adding b to a ', 
      c = float(a) + float(b)
      print 'getting', c
      return c 
  
  define_stuff()
  add_stuff()
  </code></pre>
  
  <p>if <strong>name</strong> == '<strong>main</strong>':
       main()</p></li>
  </ol>
  
  <p>I am using 2.7. Clearly "return" does not perform what I am expecting.</p>
  
  <p>What I need to do to pass a local variables within a (sub) function to main()?</p>
  
  <pre><code>returning 3.0
  adding b to a
  Traceback (most recent call last):
    File "C:\scripts\testscope.py", line 19, in &lt;module&gt;
      main()
    File "C:\scripts\testscope.py", line 16, in main
      add_stuff()
    File "C:\scripts\testscope.py", line 11, in add_stuff
      c = float(a) + float(b)
  NameError: global name 'b' is not defined
  </code></pre>