AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Global and Static Variables, When are they needed?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Larry



Joined: 03 Mar 2005
Posts: 61

PostPosted: Sun Aug 16, 2009 8:23 am    Post subject: Global and Static Variables, When are they needed? Reply with quote

Although I've been using AutoHotKey for some time now, I'm still uncertain
as to when Global and Static variables need to be used.

I'm also not real clear on the difference between them.

And, are functions the only way they can be declared. Seems like I
remember seeing somewhere that variables ending with numbers
are considered array variables and are then Global by nature.
Am I correct on that?

Thanks.
Back to top
View user's profile Send private message
YMP



Joined: 23 Dec 2006
Posts: 418
Location: Russia

PostPosted: Sun Aug 16, 2009 10:57 am    Post subject: Reply with quote

Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.

All variables declared outside any function are global. Arrays can be local.
Back to top
View user's profile Send private message
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Sun Aug 16, 2009 11:23 am    Post subject: Reply with quote

is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function?

For example, suppose my array is called extraInfo, and my functions performs a loop that sometimes adds entries to it of the form extraInfo%refNumber%:= refInfo...
_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.
Back to top
View user's profile Send private message
YMP



Joined: 23 Dec 2006
Posts: 418
Location: Russia

PostPosted: Sun Aug 16, 2009 12:31 pm    Post subject: Reply with quote

Probably not.
Functions wrote:

Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function.
Back to top
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Sun Aug 16, 2009 12:49 pm    Post subject: Reply with quote

Quote:
is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function?


Code:
SomeFunction()
ListVars
Pause
Return

SomeFunction()
{
  Global ; all array values created are global
  Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
  Loop 10
    extraInfo%A_Index% := "Field No. " . A_Index ; will be global
  declareTheOther := variablesThat ; will be local
}


HTH
Back to top
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Sun Aug 16, 2009 1:10 pm    Post subject: Reply with quote

n-l-i-d wrote:
Global ; all array values created are global
Local declareTheOther := 1, variablesThat := "you don't want to be global as local"


thanks. that's a clever workaround.
_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.
Back to top
View user's profile Send private message
Larry



Joined: 03 Mar 2005
Posts: 61

PostPosted: Sun Aug 16, 2009 3:35 pm    Post subject: Reply with quote

YMP wrote:
Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.

All variables declared outside any function are global. Arrays can be local.


So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right?

I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later?

Thanks.
Back to top
View user's profile Send private message
elisakay
Guest





PostPosted: Sun Aug 16, 2009 3:52 pm    Post subject: Reply with quote

Larry wrote:
So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right?
Wrong, the reason to declaring variables global in a function is to allow the function to access variables with the same name to be accessed outside the function - essentionally this makes sure that a variable inside and outside the function are the "same" variable. If not declared global, variables inside functions are private to the function (even if a variable of the same name is used outside the function).

Larry wrote:
I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later?
Subroutines (including Hotkey and Timer) can only access global variabes, again 'local' function variables cannot be seen/accessed outside the function where they are created.

Static variables, are simply local variables that remember their value each time the function is entered. Otherwise 'local' variables are 'empty' each time the function is entered.
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sun Aug 16, 2009 4:22 pm    Post subject: Reply with quote

@daonlyfreez

n-l-i-d wrote:
Code:
SomeFunction()
ListVars
Pause
Return

SomeFunction()
{
  Global ; all array values created are global
  Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
  Loop 10
    extraInfo%A_Index% := "Field No. " . A_Index ; will be global
  declareTheOther := variablesThat ; will be local
}


HTH


Declaring Global is redundant.. It is automatic when Local is used.
Back to top
View user's profile Send private message Send e-mail
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Sun Aug 16, 2009 4:26 pm    Post subject: Reply with quote

see if you can spot the difference:

Code:
;copy path from explorer address bar and try pasting it with the different hotkey functions
num:=1

#L::
LocalIncrement()
MsgBox % num
Return

LocalIncrement()
{
   num++
}

#G::
GlobalIncrement()
msgBox % num
Return

GlobalIncrement()
{
   global
   num++
}

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Last edited by pajenn on Sun Aug 16, 2009 4:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
YMP



Joined: 23 Dec 2006
Posts: 418
Location: Russia

PostPosted: Sun Aug 16, 2009 4:41 pm    Post subject: Reply with quote

Larry wrote:
Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later?

No, they exist within one script.
Back to top
View user's profile Send private message
elisakay
Guest





PostPosted: Sun Aug 16, 2009 4:51 pm    Post subject: Reply with quote

YMP wrote:
Larry wrote:
Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later?

No, they exist within one script.
YMP, Very nice catch, I completely/totally missed that! Embarassed
Back to top
n-l-i-d
Guest





PostPosted: Sun Aug 16, 2009 6:58 pm    Post subject: Reply with quote

Quote:
Declaring Global is redundant.. It is automatic when Local is used.


You're right, assume-global, thanks! Smile
Back to top
Larry



Joined: 03 Mar 2005
Posts: 61

PostPosted: Sun Aug 16, 2009 10:59 pm    Post subject: Thanks Everyone. Global vars are no longer a mystery to me! Reply with quote

Thanks Everyone. Global vars are no longer a mystery to me!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group