0

When I run this code, it gives 0 as output But I am calling func(a). Why it is not not assigning 1 to a

a = 0
def func(x):
    x = 1
func(a)
print(a)
Sanjay
  • 1
  • 1
  • I think you would find the answer to your question in this previous SO question https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – pastaleg Aug 18 '19 at 16:08
  • @pastaleg, In that case, shoot a close vote. – Austin Aug 18 '19 at 16:16

1 Answers1

0

This is because you are making a local variable in the function, ideally what you should be doing is this.

a = 0
def func():
    global a
    a = 1
func()
print(a)
Satomatic
  • 16
  • 2