-3

I am teaching myself python and am writing a few scripts to better understand certain things. I was wondering why this returns 0 rather than 27. If this were java I beleive it would return 27 (I initialized a variable and later called a function that changed the variable). What is going on?

result = 0;

def cube(x):
    result = x * x * x

cube(3)

print(result)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Tyler Rule
  • 11
  • 1
  • 2
    scoping. result inside function has no relation to the result variable outside. Edit: Also, there is no concept of having to "initialize" variables. Also, before you get bit by this: [names refer to values in python](https://nedbatchelder.com/text/names.html) – Paritosh Singh Feb 19 '19 at 20:02
  • avoid global variables... – Jean-François Fabre Feb 19 '19 at 20:03

5 Answers5

2

Your result variable is both defined outside the function cube as a global variable, and inside the function cube as a local variable, which is a completely different variable from the global one.

You can use the global statement to declare a variable as global for a function to update a global variable:

def cube(x):
    global result
    result = x * x * x

but global variables should be used sparingly; prefer a function that returns a value.

chepner
  • 497,756
  • 71
  • 530
  • 681
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 4
    no you should _not_ use `global`. It sucks – Jean-François Fabre Feb 19 '19 at 20:03
  • One *should* not use a global variable, but this answer is correct if one *has* to use a global. – chepner Feb 19 '19 at 20:10
  • 1
    The downvote is too harsh. However unlikely, there might be a legitimate use case for OP's question to change the `global` `result` instead. @blhsing is just answering a bad question with its answer... Although technically, it could have easily been `result = cube(3)`. – r.ook Feb 19 '19 at 20:11
  • I didn't downvote, but I closed as duplicate, as I should have done earlier. – Jean-François Fabre Feb 19 '19 at 20:17
2

The result is unchanged because of the scope. This is the right way to do it.

result = 0 # redundant

def cube(x):
    return x * x * x

result = cube(3)

print(result)

# expect 27 here
0

what you're looking for is this:

def cube(x):
    result=x*x*x
    print(result)

And then if you input cube(3) you'll get 27. You first define what your function will do, and the can call it. If you want to store a variable you end it with return result and then you can do something like:

def cube(x):
    result=x*x*x
    return result

res=cube(3)

And then, res will be stored as an int equal to 27.

Also, variables that are not specified in the return statement will always be local, so you will not be able to recover them. Also, as stated above, you don't have to initialize variables.

Juan C
  • 5,846
  • 2
  • 17
  • 51
0

Your function is not having a return, so then, print is taking the first result variable.

result = 0;
def cube(x): 
    return x*x*x   
print(cube(3))

So you dont need to use result in this case, but if you want, you can do:

result = 0;
def cube(x): 
    return x*x*x   
result = cube(3)
print(result)

I hope you answer your question, greetings!

0

to make your program work, we need to use “global” keyword. We only need to use global keyword in a function if we want to do assignments / change to a global variable.

result = 0
def cube(x):
    global result
    result = x * x * x

cube(3)

print(result)

If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace.

here you can read good explanation about scope of variables in python

however, proper way to to get the result from your function would be:

def cube(x):
    return x * x * x

result = cube(3)

print(result)
Sameh Farouk
  • 549
  • 4
  • 8