Hi, thanks for reading...
This is the question i need to answer but i dont know how to:
Compare the use and effectiveness of global and local variables. Give examples of VB code that has global and local variables.
Thank You for Your Help..Can Someone Please Try Help Me With a Programming Question?
There are a lot more disadvantages to global variables than just memory. The most important in my opinion is that *anyone* can alter global variables. So if you have a big program and you want to change a global variable (say, change from an int to a double), you have to look through *all* of your code to see who is using that global variable. With local variables, you know the scope of the variable, so you know who's able to use it.Can Someone Please Try Help Me With a Programming Question?
global variables are available from anywhere inside the program, which makes them attractive to use, but wastes memory because the memory stays allocated until the program is closed.
local variables are only available inside the current code block (procedure, function, class). They are destroyed immediately when the block of code finishes annd the memory is freed for you to reuse.
Example:
//program start
Dim name as string 'this is global
name = ';Jeff';
procedure showname()
begin
Dim name2 as string ' this is local to this procedure only
name2 = ';Chris';
msgbox(name %26amp; '; '; %26amp; name2) 'both names display
end
msgbox(name %26amp; '; '; %26amp; name2) ' error, name2 is not accessible outside the procedure
Subscribe to:
Post Comments (Atom)
c language sample programs for students
ReplyDeletec program code Create a negative from png image