title: Python recursive function to convert binary to decimal is working. I just don't understand how
body: <p>I will start this off by saying that I have not done any schooling. All of my programming knowledge has come from 12 years of doing various projects in which I had to write a program of some sort in some language.</p>
  
  <p>That said. I am helping my friend who is just getting into programming and who is taking a introductory python class. Her class is currently learning about recursive functions. Due to my lack of schooling this is the first time I have heard about them. So when she asked me to explain why the function she had worked I couldn't do it. I had to learn them myself.</p>
  
  <p>I have been looking around at various posts about solving this same problem. I found one here at <a href="https://www.geeksforgeeks.org/recursive-program-for-binary-to-decimal/" rel="nofollow noreferrer">geeksforgeeks</a> that is a function that does exactly what we need. With my elementary understanding of recursion this is the function that I would have thought would have been the right choice.</p>
  
  <pre class="lang-py prettyprint-override"><code>def bintodec(n):
      if len(n) == 1:
          bin_digit= int(n)
          return bin_digit * 2**(len(n) - 1)
      else:
          bin_digit = int(n[0])
          return bintodec(n[1:]) + bin_digit * 2**(len(n) - 1)
  </code></pre>
  
  <p>This is the function she came up with</p>
  
  <pre class="lang-py prettyprint-override"><code>def convertToDecimal(binNum):
  
      if len(binNum) == 0:
          return 0
  
      else:
          return convertToDecimal(binNum[:-1]) * 2 + int(binNum[-1])
  </code></pre>
  
  <p>When I print the function call it works.</p>
  
  <pre class="lang-py prettyprint-override"><code>print(convertToDecimal("11111111"))
  # results in 255
  
  print(convertToDecimal("00000111"))
  # results in 7
  </code></pre>
  
  <p>I understand that sometimes there is a shorthand way to things. I can't see any shorthand methods mentions in the documentation that I have read.</p>
  
  <p>The thing that really confuses me is how it takes that string and does math with it. I see the typecast for int, but the other side doesn't have it.</p>
  
  <p>This is where everything falls apart and my brain starts melting. I am thinking there is a core mechanic of recursion that I am missing. Normally that is the case.</p>
  
  <p>So along to figuring out why that works, I would love to know how this method would compare to say the method we found over at <a href="https://www.geeksforgeeks.org/recursive-program-for-binary-to-decimal/" rel="nofollow noreferrer">geeksforgeeks</a></p>