Web Analytics Made Easy -
StatCounter calling and returning - CodingForum

Announcement

Collapse
No announcement yet.

calling and returning

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • calling and returning

    I am a bit confused on calling and returning and was wondering if someone could give me a little more detail

    Code:
     
    x= 6
    
    def example():
          
            globx = x
            print(globx)
            globx+=5
            print(globx)
    
          return globx
    
    x = example()
    
    print(x)
    http://i65.tinypic.com/2mexvur.jpg

  • #2
    Hi Prayercall

    What's happening is that your function - example() is returning data instead of printing it directly e.g:

    x= 6
    def example():
    globx = x
    print(globx)
    globx+=5
    print(globx)
    print globx

    example() #we call the function.

    However - you've assigned the function to a variable, so therefore x became the caller of said function.

    so when you have x = example(), x is now equal to what example() returns, in this instance, it will be the value of globx.

    The return statement returns a value to the caller.

    So when you say print(x) what you are actually doing is assigning the value returned by example to x and then printing it to screen.

    Comment


    • #3
      When you use the return statement, The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned

      In your function, the print statement will print 6 and 11

      If you use return statement then it will end the execution of the function call and returns the result 6 and 11
      ASP.Net Software Development

      Comment

      Working...
      X