1

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.

Kasravnd
108k19 gold badges167 silver badges195 bronze badges
asked Mar 26, 2015 at 12:27

1 Answer 1

4

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!

answered Mar 26, 2015 at 12:28
Sign up to request clarification or add additional context in comments.

2 Comments

Or simply print(spam(21)).
@TimPietzcker Thanks, Have edited the answer to include your suggestion

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.