简体   繁体   中英

Understanding python recursive functions

I am new to coding and was trying to learn Python and recursive functions. I have a small program as follow:

def hi_recursive(remaining):
 # The base case
 print(remaining)
 if remaining == 0:
     return
 print('hi')

 # Call to function, with a reduced remaining count
 hi_recursive(remaining - 1)
 print(remaining)
 print('bye')

hi_recursive(3)

This results in output:

3
hi
2
hi
1
hi
0
1
bye
2
bye
3
bye

I don't understand how the value of the variable remaining increments after it is 0 and why is bye printing 3 times? Shouldn't it just print once, at the end?

I would really appreciate if you could help me understand this. Thank you.

A recursive function is a function defined in terms of itself via self-referential expressions. This means that the function will continue to call itself and repeat its behavior until some condition is met to return a result.

The reason your bye print multiple time is because when you recursing function inside your function ends, the original function that call the function inside still needs to finish. If you want your bye to print just at the end, you need to put it just before your return in your function. Also you have an extra "print(remaining)" that print your number thrice because you go over your function hi_recursive(remaining) three times.

def hi_recursive(remaining):
 # The base case
 print(remaining)
 if remaining == 0:
     print('bye')
     return
 print('hi')


 # Call to function, with a reduced remaining count
 hi_recursive(remaining - 1)
 
hi_recursive(3)

Check out this link for more info https://realpython.com/python-thinking-recursively/#recursive-functions-in-python

In you'r code:

def hi_recursive (remaining):

  print (remaining)
  if remaining == 0: ##### target #####
      return
  print ('hi')

  # Call to function, with a reduced remaining count
  hi_recursive (remaining - 1)
  print (remaining)
  print ('bye')

hi_recursive (3)

You'r smallest printed number is determined in target line . In other word if you write remaining == 0 , you'r smallest printed number is 0

You can use VScode debuger or PyCharm debuger for see Run line by line your code

Because you printed it first, then you get it This code is yours

def hi_recursive (remaining):
  # The base case
  print (remaining) # This line
  if remaining == 0: # and this line
      return
  print ('hi')

  # Call to function, with a reduced remaining count
  hi_recursive (remaining - 1)
  print (remaining)
  print ('bye')

hi_recursive (3)

Use one of these codes.

def hi_recursive (remaining):
  # The base case
  if remaining == 0:
      return
  print (remaining)
  print ('hi')

  # Call to function, with a reduced remaining count
  hi_recursive (remaining - 1)
  print (remaining)
  print ('bye')

hi_recursive (3)

or

def hi_recursive (remaining):
  # The base case
  if remaining == 1:
      return
  print (remaining)
  print ('hi')

  # Call to function, with a reduced remaining count
  hi_recursive (remaining - 1)
  print (remaining)
  print ('bye')

hi_recursive (3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM