title: Understanding how stack frames work in recursive function
body: <p>Python/CS newbie here, trying to understand how a particular recursive function works "under the hood" in terms of how the function's stack frames are operating and what values they're "holding." </p>
  
  <p>I know a lot has been written/posted about recursive functions on here, and there are illustrative examples of how they work, but the concepts of recursion and how stacks work/what they do in a recursive function are a little tricky and I'm not finding any clear and concise explanations. </p>
  
  <p>Let's say we have the following recursive function that reverses the characters in a string: </p>
  
  <pre><code>text = "hello"
  def reverse_string(text):
          if len(text) &lt;= 1:
              return text
          return reverse_string(text[1:]) + text[0]
  </code></pre>
  
  <p>which outputs: <code>olleh</code></p>
  
  <p>I'm thinking that the frames for the <code>reverse_string(text[1:])</code> work as follows:</p>
  
  <pre><code>Global frame
  text    "hello"
  reverse_string  
  
  reverse_string
  text    "hello"
  
  reverse_string
  text    "ello"
  
  reverse_string
  text    "llo"
  
  reverse_string
  text    "lo"
  
  reverse_string
  text    "o"
  
  Return
  value   "o"
  </code></pre>
  
  <p>But what happens after the value <code>"o"</code> is returned and the terminating base condition is met? How does <code>text[0]</code> work in the function to finally arrive at <code>"olleh"</code> ?</p>