Python: How does the return statement work?

  • Topic: Python 
  • Thread starter jumbogala
  • Start date
  • Tags Tags
    Python Work
3 replies · 13K views · last reply
jumbogala
Messages
414
Reaction score
4

Homework Statement


I am trying to write a program in which I have a function that uses the variable x.

For example,
def function(x):
x=x+2
return x

function(4)

Now I want to print out what x was inside the function. I thought I would just say
"print function(4)", but it's giving me an error message. "print function(x)" doesn't work either.

What am I doing wrong?
 
Are you using python 3?
The print statement was changed into a function so it would now be
print(function(4))
You can also do something like
answer = function(4)
print(answer)
 
No, I'm using python 2.4.3, I think I get the idea though.
 
jumbogala said:
I thought I would just say
"print function(4)", but it's giving me an error message.
It works for me. What was the error message?