Return Statement

Discussion in 'Python' started by sl33k_, Jan 26, 2011.

  1. sl33k_

    sl33k_ Guest

    How does "return True" and "return False" affect the execution of the
    calling function?
     
    sl33k_, Jan 26, 2011
    #1
    1. Advertising

  2. sl33k_

    rantingrick Guest

    On Jan 26, 2:26 pm, sl33k_ <> wrote:
    > How does "return True" and "return False" affect the execution of the
    > calling function?


    >>> def f1():

    pass

    >>> print f1()

    None


    >>> def f2():

    return

    >>> print f2()

    None


    >>> def f3():

    return True

    >>> print f3()

    True


    >>> def f4():

    return False

    >>> print f4()

    False


    >>> def f5():

    return 'Strawman'

    >>> print f5()

    Strawman


    ....any questions?
     
    rantingrick, Jan 26, 2011
    #2
    1. Advertising

  3. On 1/26/11 12:26 PM, sl33k_ wrote:
    > How does "return True" and "return False" affect the execution of the
    > calling function?


    It doesn't -- the value 'True' or 'False' is simply returned, and
    assigned to a name if the calling function does so explicitly. But
    there's no built in affects. If you want it to alter the execution, you
    have to do so yourself, i.e.:

    def myfun():
    return True
    def myfun2():
    return False

    if myfun():
    print "Something is true!"

    myfun2()
    print "I'm called. Cuz, the return value of myfun2 was simply discarded."

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJNQJMcAAoJEKcbwptVWx/lVDUH/3TGyob9QXqjWhr2NWUouB6+
    hcKlnUVv4yUq99cRQd6oFFwhnN7l7UvAiLibs7TwwV1Llar+4mhH6ktzIJlV7/wy
    7C8PxpNwhGj8orj6QaMPt2Ex4biwBcoWqUT/A3uPCzhgvFtscX94LGOUwxKrKU3g
    N7jxV9eSt1o9NUUai2Ol7vgk3hYc4iK0yHvjFEnAoaX0x+owTm7YaQTs4Y8K0GSk
    3zFuD1v6Ci+SCLu0NnOeWwCQgqdOmC9+qROHxaepl8BSA23APtN2lUYbJKQlaSOP
    7Nl+vLonvQiRr14+AtYSCq2hptTu8U9iy7UX930bQp5DeqcTy+rN3GWWLYkrI2Q=
    =9ict
    -----END PGP SIGNATURE-----
     
    Stephen Hansen, Jan 26, 2011
    #3
  4. On 1/26/2011 12:26 PM sl33k_ said...
    > How does "return True" and "return False" affect the execution of the
    > calling function?


    That depends on the calling function. It will control what it does next
    generally based on the returned value, but it could also simply store
    the result.

    def isACustomer(custID):
    1 return custID in currentCustList

    def isActive(custId):
    2 return custID in openInvoicesByCustID

    for custID in custList:
    3 if isACustomer(custID):
    4 activeFlag = isActive(custID)


    Here, 1 and 2 return True or False depending on the inclusion test.

    3 causes the subsequent code block to be executed when True is returned

    4 stores True of False in activeFlag for subsequent use.

    Emile
     
    Emile van Sebille, Jan 26, 2011
    #4
  5. On 26.01.2011 21:26, sl33k_ wrote:
    > How does "return True" and "return False" affect the execution of the
    > calling function?


    If only affects the calling function if you use the return value:

    def foo():
    return True

    def bar1():
    foo() # nothing difference, whether foo() returns True or False

    def bar2()
    if foo():
    print "foo returned True or any other non-false value"
    else:
    print "foo returned False or any other non-True value"
     
    Alexander Kapps, Jan 26, 2011
    #5
  6. sl33k_

    mpnordland Guest

    On 01/26/2011 03:26 PM, sl33k_ wrote:
    > How does "return True" and "return False" affect the execution of the
    > calling function?


    Basically it will affect it in whatever way you design it to for example:
    def lie_test(statement):
    if statement is True:
    return False
    else:
    return False
    Now, this is psuedo code somewhat.
    "if statement is True:" would always equate to "True" unless statement
    was an empty string, None, or 0. As to return False if statement equals
    true, look at the function name. It is testing to see if it is a lie,
    and if it is true, then it's not a lie.
     
    mpnordland, Jan 26, 2011
    #6
  7. sl33k_

    Ethan Furman Guest

    mpnordland wrote:
    > On 01/26/2011 03:26 PM, sl33k_ wrote:
    >> How does "return True" and "return False" affect the execution of the
    >> calling function?

    >
    > Basically it will affect it in whatever way you design it to for example:
    > def lie_test(statement):
    > if statement is True:
    > return False
    > else:
    > return False
    > Now, this is psuedo code somewhat.
    > "if statement is True:" would always equate to "True" unless statement
    > was an empty string, None, or 0.


    Um, no.

    Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    --> if 'some stuff' is True:
    .... print 'True'
    .... else:
    .... print "it's a dang lie!"
    ....
    it's a dang lie!

    You need either < if bool(statement) is True: >, or
    < if bool(statement): >, or, simplest,
    < if statement: >

    > As to return False if statement equals
    > true, look at the function name. It is testing to see if it is a lie,
    > and if it is true, then it's not a lie.


    Your last statement, though, should be return True -- the way you have
    it now the function always returns False.

    ~Ethan~
     
    Ethan Furman, Jan 27, 2011
    #7
  8. sl33k_

    mpnordland Guest

    I stand corrected :)
     
    mpnordland, Jan 28, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    815
  2. Jay McGavren
    Replies:
    11
    Views:
    1,155
    Alan Krueger
    Jan 16, 2006
  3. Seong-Kook Shin
    Replies:
    1
    Views:
    500
    Richard Bos
    Jun 18, 2004
  4. tedsuzman
    Replies:
    2
    Views:
    7,122
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  5. Greenhorn
    Replies:
    15
    Views:
    845
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page