0

I made a quick script below to test some behaviors after encountering a problem on a larger self learning initiative. I am using python 2.7.x.

#!/usr/bin/python
def test(arg1):
    y = arg1 * arg1
    print 'Inside the function', y
    return  y

y = int(raw_input('Enter: '))
test(y)
print 'Outside the function', y

Enter: 6

Inside the function 36

Outside the function 6

However, when the code is as below:

#!/usr/bin/python
def test(arg1):
    y = arg1 * arg1
    print 'Inside the function', y
    return  y

y = test(6)
print 'Outside the function', y

Inside the function 36

Outside the function 36

Why does the first code snippet provide 36, 6 and not 36, 36 as in the second case? What suggestions can you make for the function to return the altered value (in this case 36) so that value can be passed into another function.

For context, what I am aiming to do is have the user input a value, send that value to a function. I want that function to perform some logic upon that input, for example test to make sure it meets a certain condition: uses characters [a-zA-z -], then return that value, so that it can be passed to another function. However, I am not asking for support in this

Many thanks for your time, any help is greatly appreciated.

Franko Leon Tokalić
  • 1,457
  • 3
  • 22
  • 28
Tsunn
  • 23
  • 4
  • 4
    Python doesn't use ending semi colons, by the way – OneCricketeer Jul 28 '16 at 13:24
  • I wouldn't advise to use global variables, and even less global + locals with the same name. There are enough letters on the keyboard! – Jean-François Fabre Jul 28 '16 at 13:26
  • In the first snippet you're assigning `y` to whatever the user input is. Then you call `test` on y, but you do not assign the output of `test` to anything. So outside of the function, y is still defined as whatever the user input is. In the second snippet you assign y to the output of test, which is y it prints as 36. – Sam Jul 28 '16 at 13:26
  • 1
    Possible duplicate of [Short Description of Python Scoping Rules](http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules) – Łukasz Rogalski Jul 28 '16 at 13:27
  • @Jean-FrançoisFabre ah ok. I was just trying to work out how to pass a value from one function to another. – Tsunn Jul 28 '16 at 13:38

7 Answers7

1

case #1 explained

#!/usr/bin/python
def test(arg1):
    y = arg1 * arg1
    print 'Inside the function', y
    return  y; # returning 36


y=int(raw_input('Enter: ')) # here y is 6 as you read from user
test(y) # you are passing 6 and which computes y to be 36, though test is returning 36, but it is not stored in any variable (including y). (not re-assigned, so outside the definition/fucntion, y is still 6)
print 'Outside the function', y # so it is still 6

Case #2 explained

#!/usr/bin/python
def test(arg1):
    y = arg1 * arg1
    print 'Inside the function', y
    return  y; # returning 36


y=test(6) # here you received and stored it in y
print 'Outside the function', y # now y is 36
be_good_do_good
  • 4,311
  • 3
  • 28
  • 42
0

Because you changed the value of y on the second example by setting y = test(6). Set y = test(y) on the first and you will get the same result.

0

The only difference between the two snippets is that in the second one you reassign y to to the result of calling the function. That is all you need to do.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

In the first code, you have a global variable named  y, which you assign the value 6 to. When going into the function, you create a local variable, also called y, which you assign the value 6*6=36 to. When you leave the function, you return this value, but do not use it. Then, you print the global y.

In the second code, you assign the returned value (local y = 36) to your global value y, thus making the two printed values to be the same.

HolyDanna
  • 609
  • 4
  • 13
0

Why does the first code snippet provide 36, 6 and not 36, 36 as in the second case?

Because you defined an entirely new y variable whose value is 36, meanwhile the value outside the function is 6.

In the second case, you still have two y variables, but the one towards the bottom (almost said first one, because it technically is executed first) is the return value of the method

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You don't assign the result of test(y) the variable y in your code

y=int(raw_input('Enter: '))
# y= 6
# here you need to assign y= test(y)
test(y)
# test result is 36, but not saved to y
print 'Outside the function', y
# print 6
user2853437
  • 750
  • 8
  • 27
0

There are two reasons the output is not the same: - the scopes are different, in your first example, the y you assign in the function is not the same the y outside of it, even if they have the same name. To (over)simplify, you can think that, when you start a new function, there is a brand new memory space created and, inside of it, there is nothing already present (except the modules names imported, the argument to the function, ...), so, even if you do an assignation to y inside the function, it is a brand new y, and, its life stops when the function is over. - in the second example, you take to output of your function and put it in y, in the first example, the output is lost since not used.

To illustrate my first point:

#!/usr/bin/python
def test(arg1):
    y = arg1 * arg1
    print 'Inside the function', y
    return  y;
test(3)
print y

This will raise NameError, because, outside the function, y does not exists.

I invite you to read and understand this very simple explanation Getting Started with Python - Variable scope

potens
  • 15
  • 4