+ 1
why the answer is 5 and not 7 ???
x= 5 def funA(x,y): x=7 return x funA(x,4) print(x)
5 Answers
+ 2
varables named x in your first and your third line are different variables, you can have two with the same name because they are not in the same scope. so, when you assign 7 to x, you are actually assigning 7 to x from funA and not the global one. when funA is called, value of the global x is copied to the x which is the argument of the function. this code is equivalent to yours:
x_global = 5
def funA(x_funA,y):
x_funA=7
return x_funA
funA(x_global,4)
print(x_global)
0
7
0
@Ranjith kumar, No iam sure it is 5 i am already tried it and the answer was 5
0
@RedAnt, thanks alot