4.1.1.13/14 Local variables in subroutines / Global variables in a programming language Flashcards Preview

Computing Theory > 4.1.1.13/14 Local variables in subroutines / Global variables in a programming language > Flashcards

Flashcards in 4.1.1.13/14 Local variables in subroutines / Global variables in a programming language Deck (5)
Loading flashcards...
1
Q

What are the differences between global and local variables?

A

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.

2
Q

What will happen if a global and local variables are assigned the same name?

A

An error will occur, or within the procedure the local rather than the global variable will be accessible. This could produce errors.

3
Q

Why may the use of global variables produce bugs?

A

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.

4
Q

Why should routines ideally not use global variables?

A

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

5
Q

What is a local variable?

A

A variable declared in a subroutine used exclusively within that subroutine

Decks in Computing Theory Class (102):