What do you understand by local and global scope of variables? How can you access a global variable inside the function, if function has a variable with same name.

 

Answer:

A global variable is accessible globally ie., it is declared outside any function and it can be accessed from any function in the program. 

A local variable is accessible only in the function where it is declared ie., it is declared inside a function and can be accessed only from that function.

To access a global variable in a function, if the function has a local variable with the same name, we use the global keyword before the variable name.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.