4.1.1.13/14 Local variables in subroutines / Global variables in a programming language Flashcards
What are the differences between global and local variables?
A global variable is accessibly from anywhere within the program, a local variable is only accessible in the subroutine it is declared in. Global variables exist and use memory throughout the program’s execution.
What will happen if a global and local variables are assigned the same name?
An error will occur, or within the procedure the local rather than the global variable will be accessible. This could produce errors.
Why may the use of global variables produce bugs?
Using the same name for local and global variables can cause errors. And it is more difficult to follow the use of global variables so it’s value may have been changed inside a subroutine without the programmer noticing.
Why should routines ideally not use global variables?
Without global variables it is easier to reuse subroutines in different programs as the subroutine is independent from the rest of the program. Using them makes the program less understandable and harder to debug, as it may produce undesirable side effects
What is a local variable?
A variable declared in a subroutine used exclusively within that subroutine