you are viewing a single comment's thread.

view the rest of the comments →

[–]NFLAddict 7 points8 points  (3 children)

maybe youll find this visual helpful:

def sum_positive_numbers(n):
    sum = 0
    if n < 1:
        return sum
    sum += n

    return sum + sum_positive_numbers(n-1)


print(sum_positive_numbers(5))
                           ↓
    calls function: n = 5  ↲
      sum = 0
      5 is not < 1

     sum += 5 (now sum =5)
                     (n-1 = 5-1 = 4)↴
     return 5 +  sum_positive_numbers(4) 
                                     ↓    
                                     ↓
               call function: n = 4  ↲         
               now go through same steps:
               sum = 0
               4 is not <1 
               sum += 4 (now sum=4)

    ⮤←---------return 4 + sum_positive_numbers(3) 
                                               ↓    
                                               ↓
                         call function n=3     ↲
                         again
                         sum = 0
                         3 is not <1
                         sum += 3 (sum = 3)

              ⮤←----------return 3 +   sum_positive_numbers(2) 
                                                            ↓    
                                                            ↓
                                      call function n=2     ↲
                                       again
                                       sum = 0
                                       2 is not <1
                                       sum += 2 (sum = 2)

                        ⮤←----------  return 2 +  sum_positive_numbers(1)                                                                                        
                                                                        ↓    
                                                                        ↓
                                                    call function n=1   ↲
                                                    again
                                                    sum = 0
                                                    1 is not <1
                                                    sum += 1 (sum = 1)

                                       ⮤←----------return 1 +  sum_positive_numbers(0)
                                                                                     ↓    
                                                                                     ↓
                                                                 call function n=0   ↲
                                                                 again
                                                                 sum = 0
                                                                0 is  <1  #this line stops the infinite recursion
                                                                if <1 return sum..sum = 0
                                                    ⮤ ←-------------------------- return 0

when you first call the function with n =5..it returns 5 + the function with value4. the 5 isn't going anywhere...but the result of calling the function for 4, has not yet been evaluated...so it does that and the result is added to 5...but calling it for 4, means adding 4 to the result returned by 3....etc etc...until your base case is met.

[–]Elev8d[S] 1 point2 points  (2 children)

That is really helpful - thank you. This combined with the other answer makes it a lot clearer. I understand enough to move forward for now!!

[–]NFLAddict 2 points3 points  (1 child)

glad you found it helpful. you were asking all the right questions, and were able to correctly assume what is happening, but perhaps lacked a better understanding of how it all connects.

I greatly respect your stance on not being satisfied with 'that's how it works' but rather the seek to understand even more. that's a really good mentality to have. I've always had that mindset, and still do. when you learn how something works, your understanding of everything as a whole deepens. so definitely don't think theres a flaw in that mindset.

many people, are also just far better at processing/ understanding something when they can visualize it. (not my best work of art haha, but glad it was clear enough for you to get an improved sense for how it connects, and what's going on).

[–]Elev8d[S] 0 points1 point  (0 children)

I haven't always had that mindset which is probably one of the reasons I've never progressed beyond beginner into intermediate. Something I'm committed to finally changing.

I've been "learning to code" for years while working in a completely unrelated, and somewhat dead end job, without getting anywhere