Ad

What Happens When You Call A Function In It Self?

- 1 answer

I saw an example online that looks like this:

 def DecimalToBinary(num):

      if num >= 1:
         DecimalToBinary(num // 2)
      print(num % 2, end = '')

print(DecimalToBinary(12))

I'll be happy if someone explain it to me.

Ad

Answer

It is called recursion. A type of concept of function calling itself. If it has no proper if statement that terminates the function itself, loop will not end or it will error. In your case it terminated.

01100None
Ad
source: stackoverflow.com
Ad