Local and Global Variables

A local variable is declared (set up) and only available for use in the sub-routine it is made in. A global variable is declared at the start of your program (in VB anyway) and can be used anywhere.

It is easier to understand when you look at some code:
private sub get_marks

dim marks(10) as integer

for counter 1 to 10

marks(counter) = inputbox(“Type in your mark”)

next

end sub

In the code above the array (marks) is only available inside the sub-routine called get_marks. If you try to access it somewhere else, it won’t work i.e. the scope of the variable (array) is local

If you set up the variable (or array) at the start of the program before any sub-routines (like we normally do) then it will be available for use everywhere i.e. the scope of the variable is global

Notes below:

local-global-variables.jpg

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: