Python Forum
Return not exiting function??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return not exiting function??
#1
Hello,
I have this simple example where I simple want to exit a function when i == 5.
def foo(i):
    print i, "--"
    if i < 5:
        foo(i+1)
        print "++++++"
    elif i == 5:
        print "xxxxxx"
        return "what ever"
        
    print "why is this printing?"
    
x = foo(3)
print x

# Result:
3 --
4 --
5 --
xxxxxx
++++++
why is this printing?
++++++
why is this printing?
None <-----
2 things I don't get. 1-why is it not exiting the function? The fact that it is printing the last line shows that it is not exiting after return right? 2-(which I guess is derived from 1), why x is None when the return is "what ever"
I always use return to exit, but I an not sure what is happening here.

thx
Reply
#2
you call the function with argument i==3. It execute the if part (i.e. prints ++++++), because you call the function recursively when you call foo with i==5, it returns the what ever. You just throw it away and then it continues on line 10 and prints "why is this printing?" and then it exit the function (and return default None, because there is no explicit return.
It NEVER enters the elif block and never execute line 8 (where the only [explicit] return in the function is).
Try this site http://www.pythontutor.com/visualize.html
to visualise the execution and get better understanding
And by the way, don't use python2, use python3 instead. python2 is dead.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
okey, I see, so it exiting the recursion and continues with the rest. Got it, that was silly Wall
Thank you
Reply
#4
You are calling the function more than once.

Sometimes when you run the function, the elif is taken, the "xxxxx" prints, and the function exits.
Then another time when the function is called the elif is skipped and the "why is this printing" appears.
rudihammad likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 608 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,276 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,736 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How to invoke a function with return statement in list comprehension? maiya 4 2,821 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,435 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 17,026 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
Question exiting the outer function from the inner function banidjamali 3 3,519 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Why does my function return None? vg100h 3 2,194 Oct-29-2020, 06:17 AM
Last Post: vg100h
  how to keep a Popen instance existant in a function return? Skaperen 7 3,131 Sep-17-2020, 07:10 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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