1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: How does the return statement work?

  1. Mar 17, 2009 #1
    1. The problem statement, all variables and given/known data
    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?
     
  2. jcsd
  3. Mar 17, 2009 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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)
     
  4. Mar 17, 2009 #3
    No, I'm using python 2.4.3, I think I get the idea though.
     
  5. Mar 18, 2009 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It works for me. What was the error message?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python: How does the return statement work?
Loading...