zychin
zychin

Reputation: 65

Function returns as error (python 3)

I am kinda new to Python, and I would like to ask a question :

def spam(a):
    a = 1 + a
    return a
spam(21)
print(spam)
input()

After running it, the output is function spam at 0x021B24F8. Shouldn't the output be 22? Any help will be appreciated.

Upvotes: 1

Views: 80

Answers (1)

Bhargav Rao
Bhargav Rao

Reputation: 52081

The problem is that your function, i.e. spam is returning a value. You need to accept the value returned by the function and store it in a different variable as in

s = spam(21)
print(s)

Here, you will store the returning value in the variable s which you will print it out.

After making the correction, the program will print as expected as in

22

Note - As mentioned, having a single statement print(spam(21)) also works as spam(21) will return 22 to the print function which will then print out the value for you!

Upvotes: 4

Related Questions