return keyword

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pyn00b
    New Member
    • Feb 2008
    • 6

    return keyword

    In a Python function definition, sometimes you don't see the return keyword at all. Sometimes, you see it all by itself. And sometimes, you see it associated with a bunch of variables - like "return x, y, z".

    Can someone clearly explain the differences between these 3 scenarios? What exactly is happening? What is the Python keyword "return" for?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by pyn00b
    In a Python function definition, sometimes you don't see the return keyword at all. Sometimes, you see it all by itself. And sometimes, you see it associated with a bunch of variables - like "return x, y, z".

    Can someone clearly explain the differences between these 3 scenarios? What exactly is happening? What is the Python keyword "return" for?
    Let's say you have the following funtion:
    [CODE=python]
    def xfive(inp):
    tmp = 5
    result = tmp * inp
    return result

    # Call the function like this (>>> means this was done from the shell)
    >>> xfive(3)
    15
    [/CODE]

    This function takes an input, and returns a result... although it doesn't have to take an input... it could just as easily be the following:
    [CODE=python]
    def threexfive():
    tmp = 5
    result = tmp * 3
    return result

    # Then you would call the function like this
    >>> threexfive()
    15
    [/CODE]

    When a function doesn't have the return statement at the end, it still "returns" to the calling function, however it does no return a value.

    When using a mutli-variable return such as:
    [CODE=python]
    def ret3(inp):
    tmp = 5
    tmp2 = 2
    result = tmp * inp
    return result, tmp2, tmp

    # Call the function and receive three elements back
    >>> mylist = xfive(3)
    >>> mylist
    [15, 2, 5]
    [/CODE]

    Basically, you can return any number of elements that you would like.

    Does that clear it up a little bit? I suggest referring to the :Python documentation if you would like more explanation of basic programming techniques. You can find it here: http://www.python.org/doc/

    Oh, and btw... if you see simply return with no variables associated it's the same thing as not having a return statement at all. It's just a matter of preference.

    Comment

    • hidrkannan
      New Member
      • Feb 2007
      • 32

      #3
      [CODE=python]
      def func1():
      return

      def func2():
      return 5,6,7
      def func3():
      print 5

      if __name__ == '__main__':
      a = func1()
      b = func2()
      c = func3()

      print type(a), a
      print type(b), b
      print type(c), c

      [/CODE]

      Above code has the 3 scenarios you have mentioned.

      func3 does not have a return statement
      func1 has a return statement by itself
      func2 has a return statement with 3 integers returned

      In python, function returns one and only object.

      In the above code the returned objects are stored in the variable a, b & c.

      Be default any function returns "NoneType" object which is the case for func3 & func1

      In case of func2, it returns "Tuple" object. return 3,4,5 is equivalent to return (3,4,5).

      SKN

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        Additionally:
        [CODE=python]
        >>> a, b, c = func2()
        >>> a
        5
        >>> b
        6
        >>> c
        7
        [/CODE]
        You could assign each element of the tuple to its own variable without needing to bother with storing the entire object

        Comment

        • pyn00b
          New Member
          • Feb 2008
          • 6

          #5
          Originally posted by jlm699
          Does that clear it up a little bit? I suggest referring to the :Python documentation if you would like more explanation of basic programming techniques. You can find it here: http://www.python.org/doc/

          Oh, and btw... if you see simply return with no variables associated it's the same thing as not having a return statement at all. It's just a matter of preference.
          Thank you! Yes, this is clearer to me now. So no return statement means that no value is returned (after reading elsewhere a little I found this means it returns a value of None). Anything more than 1 variable being returned results in a list being returned. As well, I didn't know that no return and a return without a variable are the exact same thing.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by pyn00b
            ..snip.. Anything more than 1 variable being returned results in a list being returned. ..snip..
            Actually a tuple is returned. Tuples are similar to lists but have very distinct differences.

            Comment

            Working...