Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

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!

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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... ;-)

OpenStudy (anonymous):

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

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!