Global and Local Variables

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

GLOBAL AND LOCAL VARIABLES

Global Variables
Global variables are approached by any function that is part of the main program. They are put into
play by combining memory locations with the names of the variable. Unlike local variables, global
variables do not come back into play each time the function has been called back. Global variables
are used when you need the value outside of the scope that it was originally created in.

Local Variables
Local variables are only existent within the exact function that creates them. They are otherwise
unknown to the other functions and the main program. Local variables stop once the function which
created them has been ended. They do however come back into play each time the function has been
executed. A local variable only exists for its scope. Local variables are usually only needed in one
section of code and are only modified within that section.

Global and variables declared in my programs


Global and local variables that were included in my UCAS calculator included the following:

Dim PassUnitPoints As Integer


Dim MeritUnitPoints As Integer
Dim DistinctionUnitPoints As Integer

The above are examples of local variables that have been declared inside of a subsided module.
They are only used within that specific module.

Global and local variables that were included within my notesaver included the following:

Dim saverfile1 As New SaveFileDialogue


saverfile1.DefaultExt = "*.doc"

The above are examples of local variables in my notesaver, known as procedural variables. The
reason for this is that it is not limited to a certain section of the program, instead it can be used
anywhere in the program

Scope of Variables

The scope looks at the visibility of a variable, where it is in the program, can it be seen and used. An
example could be that in my note saver program:

Dim saverfile1 As New SaveFileDialogue


saverfile1.DefaultExt = "*.doc”,

The above variable is declared inside of the event handler and can only be used inside of that event
handler; it does not exist outside of it. This is a local variable.

A variable that is declared within the form class can initially be used within the event handlers, it will
have a wider scope.
The words private and public are used to identify variables that are accessible only inside their class
or even from outside. The name for this is an access modifier.

Modules

Variables that are separately declared in modules are only limited to that one module. An example
of this can be seen in the following code from my UCAS calculator:

Public Class BND

Dim PassUnitPoints As Integer

End Class

You might also like