[Web4lib] global variables versus local variables

Vishwam Annam vishwam.annam at wright.edu
Mon May 23 13:17:12 EDT 2005


I use local variables, when I want to restrict them to be accessed only 
with in a subroutine or a class. I am not sure, whether declaring 
variables  as local makes your program more efficient or not, but it is 
good idea to use them for temporary or with-in subroutine(function) 
computations. 

About  the for loop, if you have one like, for (int i=0;i<10;i++) this 
doesn't define "i" 10 times, it just increments it's value each time by 
one. If you have some thing like  for (int i=0;i<10;i++)  { int j=0; } 
then "j" is defined and initialized to 0 ten times.

I use global variables if I want to use their values anywhere throughout 
my program.

Vishwam
Wright State University Libraries

Karen Coyle wrote:

> When I was learning programming that had local and global variables, 
> the local variables were billed as being more "safe" because other 
> routines could not inadvertently modify or delete them. You chose 
> local or global depending on the role of the data element in relation 
> to the sub-routines, not based on efficiency. And with the speed of 
> today's computers, the difference in efficiency between a global and a 
> local variable may not be significant for most applications.
>
> Others can undoubtedly explain this better than I.
>
> kc
>
> John Fitzgibbon wrote:
>
>> Hi,
>>
>> When programming and when writing scripts, we are encouraged to use
>> variables that are local in scope whenever we can.
>>
>> However, defining a variable within a sub-routine, as in VbScript, means
>> the variable is defined every time the sub-routine is run whereas
>> defining it outside the sub-routine means the variable is defined only
>> once. Why is it more efficient to define a variable within a
>> sub-routine?
>>
>> Also, defining a variable within a loop causes the variable to be
>> defined afresh with each instance of the loop. Again this appears
>> inefficient but is generally done when a 'for loop' is run in
>> JavaScript.
>>
>> I don't doubt that local variables have their uses; I'm simply seeking
>> an explanation.
>>
>> Regards
>> John
>>
>> John Fitzgibbon
>>
>> John Fitzgibbon
>>
>> Galway Public Library
>> Island House
>> Cathedral Square
>> Galway
>> Ireland
>>
>> p: 00 353 91 562471
>> f: 00 353 91 565039
>> w: http://www.galwaylibrary.ie
>> ******************************************************************* 
>> Tá eolas atá príobháideach agus rúnda sa ríomhphost seo agus aon 
>> iatán a ghabhann leis agus is leis an duine/na daoine
>> sin amháin a bhfuil siad seolta chucu a bhaineann siad. Mura seolaí 
>> thú, níl tú údaraithe an ríomhphost nó aon iatán a ghabhann leis a 
>> léamh, a chóipáil ná a úsáid. Má tá an ríomhphost seo faighte agat 
>> trí dhearmad, cuir an seoltóir ar an eolas thrí aischur ríomhphoist 
>> agus scrios ansin é le do thoil.
>> This e-mail and any attachment contains information which is private 
>> and confidential and is intended for the addressee only. If you are 
>> not an addressee, you are not authorised to read, copy or use the 
>> e-mail or any attachment. If you have received this e-mail in error, 
>> please notify the sender by return e-mail and then destroy it. 
>> *********************************************************************
>> _______________________________________________
>> Web4lib mailing list
>> Web4lib at webjunction.org
>> http://lists.webjunction.org/web4lib/
>>
>>
>>  
>>
>



More information about the Web4lib mailing list