-1
def double(n):
   global y
   y = 2 * n

y = 5
double(y)
print(y)

Confused with the output why is it 10 and not 5?

Cohan
  • 4,384
  • 2
  • 22
  • 40
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Cohan Jan 11 '20 at 17:08

1 Answers1

0

The global keyword allows a user to modify a variable outside of the current scope, so when use global y, y change outside of the function. if remove a global keyword the output will be 5.

Leon
  • 24
  • 3