Python Forum
Information "creeps up" in recursive function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Information "creeps up" in recursive function
#1
Hello everyone!
I'm a beginning programmer trying to write a program that solves sudoku grids using a recursive function (no, I don't want you to tell me how it's done).
I'm absolutely baffled by the way it behaves. I'm sure I must be missing something, but superficially it looks like the values of variables are getting fed back to the calling instance of the recursive function instead of being "forgotten" when the function returns.
When the program terminates, the grid at recursion depth = 2 is full of values generated by the program.
This is not normal. At depth= 2 the grid should contain at most 2 guesses/deductions by the program. All those values were generated at depths >> 2. So how the hell does the function "know" about them at depth=2. I just don't get it Wall I assume I must have done something really stupid...
Reply
#2
If you could provide minimal code to reproduce the issue, that would help us a lot. Don't forget to use code tags :)
Reply
#3
Recursion can be a challenge but Python handles it nicely. Without a post of code, let's take a general example:
def call_me(progress):
    progress+=1
    if progress == 100 :
        return(progress)
    else:
        print(f'In Progress {progress}')
        call_me(progress)
        print(f'Unwinding {progress}')
        
print(call_me(5))
When you print call_me(5), it calls the call_me function. This increments the number sent from 5 to 6, compares to 100, if less then call_me calls itself which increments and calls itself, recursively until the number reaches 100. Then it returns. Where does it return to? The line after the call_me statement in the if. So, it unwinds.

Each time that call_me calls itself it pushes the current values of the variables on a stack. When the program returns to the function it pops the current values off the stack. So, the variable progress "inexplicably" chases back down, undoing all the work you did to get it up to 100.

Make some sense?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 580 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,252 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,381 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,011 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,697 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,914 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Recursive function returns None, when True is expected akar 0 3,391 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial using recursive function spalisetty06 12 4,074 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  Recursive Function sridhar 7 2,835 Jul-14-2020, 07:53 PM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,262 May-09-2020, 06:09 PM
Last Post: Etotheitau

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020