A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

How do you return the value of a variable from a function? For example, I want to use variable x. Say I start my program with "x=1". Inside the function I have the statement x=x+1. However, when the function ends and I say "return x", if I have "print x" outside of that function, the value of x is still 1. How do I initialize the variable outside of the function, allow the function access to this variable, and then return the new value once the function ends? Or in simpler terms, with the function stated above, how do I get x to be 2 outside the function? Thanks!

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The easiest way is to assign x to the result of the function. so say we have a function called addOne: def addOne(x): x += 1 return x If I wanted to make the variable x equal the same thing outside the function as it did at the end of the function, I would assign it where I wanted to use it as x = addOne(somenumberorvariable) That way, the variable x wherever I am gets bound to the result of the function addOne. But it's important to remember that the variable inside addOne is local, meaning that it goes away when that function ends. You can have a variable outside the function that happens to have the same name as the variable inside the function, but those are two different variables.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The exception (and it might take someone brighter than me to explain why exactly it's an exception) is mutable data types. If you change the value of an item in a list, or a key in a dict from within a function, it will be changed outside the function too. I suppose if you really wanted, you could use a list with one element outside the function, work with the value of yourList[0] within the function, and be able to use the modified value outside the function too. That seems like a bad way to do things though.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    From what I understand, and if I am wrong please indicate so, so that I may get things right: That is because the variable inside the function points to a specific area in memory - and is like a BOX of Values at a set location. If you simply alter them, with a function like append, that same place in memory becomes that altered BOX, so wherever you call that BOX, inside the function or not, it returns its value. If you change the variable completely, by assigning it a new value (and not just changing it) then the variable points to a different place in memory, and the BOX will no longer be attached to that variable. Clear as Mud... ;-)

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    In Python, variables are just names for objects. This is in contrast to other programming languages where a variable defines both a name, and a "space" to store a value. In python a variable is just a name. In other languages (C for example) int x = 1; creates a new "box" called x that holds the value 1. In python x = 1 takes the integer object 1 and gives it the name x. So x += 1 in C changes the value stored in the box labled X, while in python x += 1 gives the integer number 2 the name x. (This has the side effect of removing the name we used for the integer object 1). Here's some good info: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.