axellbrendow
axellbrendow

Reputation: 295

This variable has not been assigned a value (a global variable)

I just discovered that if I try to use a global variable inside a function that is declared after a simple hotkey, a warning appears saying that this global variable doesn't has a value.

Illustration:

code and warning image

In this example, when I press Shift + l the warning appears.

Can anyone explain ?

Upvotes: 1

Views: 5820

Answers (1)

Relax
Relax

Reputation: 10568

Variables have to be declared in the auto-execute section or within a hotkey/hotstring/or another function.

#Warn
global a := "10/10"  ; super-global variable

$+p:: Pause

$+1:: foo()

foo(){  
    MsgBox % "a = " . a
}

or to access global variables within a function you need to add global within the function:

#Warn

a := "10/10"  ; global variable

$+p:: Pause

$+1:: foo()

foo(){
    global
    MsgBox % "a = " . a
}

or:

#Warn

$+1:: 
    global a := "10/10"
    foo()
return

$+p:: Pause

foo(){
    MsgBox % "a = " . a
}

For more details read Local and Global Variables

Upvotes: 5

Related Questions