Skip to content Skip to sidebar Skip to footer

How To Return An Int Value From A Function Python

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 aga

Solution 1:

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

Solution 2:

By using "return" you can return it out of the function. In addition you can specifie the datatype.

For Example:

def myFunction(myNumber):
    myNumber = myNumber + 1
    return int(myNumber)

print myFunction(1)

Output:

2

You can also display the datatype which you got returned out of the function with type()

print type( myFunction(1) )

Output:

<type 'int'>

Post a Comment for "How To Return An Int Value From A Function Python"