Python: How does the return statement work?

  • Context: Python 
  • Thread starter Thread starter jumbogala
  • Start date Start date
  • Tags Tags
    Python Work
Click For Summary

Discussion Overview

The discussion revolves around understanding the return statement in Python functions, specifically how to print the value returned by a function. The context includes a homework-related inquiry regarding function behavior in different versions of Python.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant describes a function that modifies a variable and attempts to print its return value, encountering an error.
  • Another participant suggests using the correct syntax for the print function in Python 3, indicating that the print statement has changed.
  • A different participant clarifies they are using Python 2.4.3, implying that the syntax issue may not apply to their situation.
  • One participant requests clarification on the specific error message encountered when trying to print the function's return value.

Areas of Agreement / Disagreement

Participants have differing views on the syntax for printing function results based on the version of Python being used, indicating a lack of consensus on the issue.

Contextual Notes

The discussion highlights potential confusion arising from differences between Python 2 and Python 3, particularly regarding the print statement and function behavior.

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?
 

Similar threads

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