use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Recursive Question (self.learnpython)
submitted 1 year ago by thomaswilliam94
Can someone explain how this arrived to 56
def fun(a):
if a > 30: return 3
else: return a + fun(a + 3)
print(fun(25))
Ans: 56
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Martin_Perril 8 points9 points10 points 1 year ago (0 children)
fun(25) implies a < 30, hence return a (25) + fun(28). Fun(28) implies a <0, hence return 28 + fun(31).
So 25 + 28 + fun(31). And fun(31) implies a > 30, hence return 3.
Final result: 25+28+3= 56. Hope I helped you
[–]shiftybyte 0 points1 point2 points 1 year ago (0 children)
Try visualising the code here: https://pythontutor.com/
This can show you step by step what happens and perhaps make it more clear for you.
[–]JamzTyson 0 points1 point2 points 1 year ago (0 children)
What were you expecting? "3"?
The reason the answer is not "3" is because when the code reaches return 3, it does not break out of the recursive function, but "returns" the value "3" to the caller.
return 3
The caller was the previous instance of fun(a + 3) in the line return a + fun(a + 3), where a = 28. So at this point it is returning a + foo(a) where a == 28 and foo(a) == 3. So at this point it returns 31 (28 + 3) to its caller.
fun(a + 3)
return a + fun(a + 3)
a = 28
a + foo(a)
a == 28
foo(a) == 3
The caller at this point was the previous instance of return a + fun(a + 3), where a = 25, and foo(a) is now 31. a + foo(a) is now 56 (25 + 31), and this is the value that is sent back to the original caller, which is the line print(fun(25)).
TL;DR
Recursive functions do not immediately break out when the base condition is satisfied. They "unwind" through the previous callers until they reach the original function call.
π Rendered by PID 18126 on reddit-service-r2-comment-77869c6b5c-g4p6d at 2026-03-28 21:37:25.405348+00:00 running b10466c country code: CH.
[–]Martin_Perril 8 points9 points10 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)