Python Python: How does the return statement work?

  • Thread starter Thread starter jumbogala
  • Start date Start date
  • Tags Tags
    Python Work
Click For Summary
The discussion revolves around a programming issue related to printing the output of a function in Python. The user is trying to print the result of a function that modifies a variable, specifically using the syntax "print function(4)", but encounters an error. A participant clarifies that in Python 3, the print statement has been changed to a function, requiring the use of parentheses: "print(function(4))". Another suggestion is to store the function's return value in a variable before printing it, such as "answer = function(4)" followed by "print(answer)". The original poster mentions using Python 2.4.3, indicating a potential compatibility issue with the print function syntax. The conversation highlights the differences between Python versions and the correct usage of print statements.
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?
 
Technology news on Phys.org
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?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
Replies
1
Views
2K
Replies
55
Views
6K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K