The first time you used X was on that def. This means that X became a variable in the scope of that function. Once the function return X cease to exist. If you use it again, then X will have the default value (0). You should declare the variable with var or dim before using them to know in wich scope they are(Global or local)