[SOLVED] Call a function in it self?

Issue

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.

Solution

This is called recursion. There is no problem as long as there is a break condition, in other words the calls to itself stop at some point. Otherwise you have an infinite loop.

Answered By – Philip Z.

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *