开发者

Why it is showing different output?

x=4    
def func():           

       print("HELLO WORLD")          
       y=x+2    
       x=2    
       print (y)          
       print (x) # OUTPUT IS 6,2,2
       global x  # global declaration is done here
func()   
print (x) # outputs as 2 but why???? why not 4????

Why it shows the output as 6,2,2. Indeed i made the print (x) before the global declaration.But i didnt change x value after global declaration but why its printing the x value as 2 after the func().Is it n开发者_运维知识库ot a sequential execution of statements? or does it read the entire code in the function and then start executing the function line on line? please clear the above program.Thank you in advance


The global keyword indicates to the compiler that the variable is to be considered global throughout the function. It doesn't matter where it shows up in the function, so long as the compiler takes note of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜