How to return an int value from a function python

51,036

To return a value, you simply use return instead of print:

def showxy(event):
    xm, ym = event.x, event.y
    x3 = xm*ym
    return x3

Simplified example:

def print_val(a):
    print a

>>> print_val(5)
5

def return_val(a):
    return a

>>> result = return_val(8)
>>> print result
8
Share:
51,036
Pstie
Author by

Pstie

Updated on May 25, 2020

Comments

  • Pstie
    Pstie almost 4 years

    I am really new to Python and found this snippet online that I've modified, right now I have it printing x * y but I want to be able to return it as a int value so I can use it again later in the script.

    I'm using Python 2.7.6.

    def show_xy(event):
        xm, ym = event.x, event.y
        x3 = xm * ym
        print x3
    root = tk.Tk()
    frame = tk.Frame(root, bg = 'yellow', 
                    width = 300, height = 200)
    frame.bind("<Motion>", showxy)
    frame.pack()
    
    root.mainloop()
    

    Kind regards, Postie

  • Pstie
    Pstie about 10 years
    It must be a problem with my layout then as everything works but it looks like the value cant reach anywhere else in my script. Thanks for the help
  • sshashank124
    sshashank124 about 10 years
    @PadraicCunningham, I like your sarcasm. I did it myself. :)