Local Variables Flashcards

All 3 cards in this deck

What is a local variable?

A variable declared inside a subroutine. It usually only exists while the subroutine is executing and can only be accessed within that subroutine.

True or false? A variable declared inside a subroutine can still be used in the main program after the subroutine ends.

False. It is a local variable, so it only exists while the subroutine is executing.

Why is it good practice to use local variables in subroutines?

Any two of: changes inside the subroutine can't accidentally affect variables elsewhere; the same name can be reused in other subroutines; the subroutine is self-contained, so it is easier to test and reuse; its memory is freed when it ends.