[Type A] Working with Functions – Chapter 3 Sumita Arora
Table of Contents
11. What is the difference between local and global variables?
Solution:
Local Variables | Global Variables |
---|---|
Defined within a specific function. | Defined outside of any function. |
Scope is limited to the function in which they are defined. | Scope extends throughout the entire program. |
Changes made to them are local to the function and do not affect other parts of the program. | Changes made to them affect the entire program, unless shadowed by local variables with the same name. |
In short, variables defined inside a function are local variables. They are accessible only within that function while variables defined outside of any function or declared global inside a function are global variables. They can be accessed from any part of the program.