4.1.1.13/14 Local variables in subroutines / Global variables in a programming language 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a local variable?

A

A variable declared in a subroutine used exclusively within that subroutine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly