Jump to content

Declaring Global Variables in Functions


Recommended Posts

I have several variables that are used throughout my script by multiple functions. If I define them at the top of the script outside of any function using the keyword DIM, SciTE doesn't throw any errors. If I define them within a function using the keyword GLOBAL, unless I redefine them in all functions that they are used in, SciTE throws a - WARNING: $variable: possibly used before declaration. error.

What's the accepted way of declaring global variables - at the top of the script or multiple times throughout functions that they are used in?

Also, will opt("MustDeclareVars", 1) have any effect on how/where variables are declared?

Thanks

Link to comment
Share on other sites

In my programming experience.

If you want a global variable you declare it in the global space (top of your script) otherwise the variable should be local.

Not sure I explained that well.

Edit: Using Local and Global is more organized and if used right can help you keep everything easy to read. I am trying to get into the swing of this as using Dim is just I guess an easy way to do it if you dont know whether it should be Global or Local. If you know use it. Trust me it will help in the future. Though in AutoIt I dont believe (though I could be mistaken) it has much of a difference.

Let me know,

JS

Edited by JSThePatriot

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

i like using ... DIM because i am able continue scripting and there are no errors

opt("MustDeclareVars", 1) clearly "you must declare all variables"...so

if you are gojng to write a large script that uses alot of memory etc, you should use that option

otherwise, use Dim and create what you want

8)

hobbiest approach

8)

NEWHeader1.png

Link to comment
Share on other sites

Yeah, thanks guys.

I've been using DIM to declare all variable at the top of my script, however, as my script is now becoming rather large, I am wanting to start declaring local variables inside functions so they are destroyed on return and my script becomes less memory intensive.

What's the difference between declaring a variable GLOBAL (as opposed to using DIM) outside of functions, do you know? Is there any benefit other than it give a visual indication that the variable is global in nature?

Cheers

Link to comment
Share on other sites

Scope

A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.

If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.

By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.

I pulled that from the helpfile. Scope is the term I was racking my brain for.

Dim'ng inside a function is Local. Dim'ng outside is Global. Basically that is what the above explains. It also explains the exceptions.

I hope the above helps answer your last question.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Yeah, I read that prior to posting thank JS. It didn't really explain everything I was after but it did help me to decide to use local variables inside functions to cut down on memory use.

One of the things that confused me was, according to how I read that paragraph, if I declared a variable as GLOBAL inside a function, this should have the same effect as if I declared the variable outside the function using DIM. This doesn't appear to be the case, hence, my post.

Link to comment
Share on other sites

Yeah, I read that prior to posting thank JS. It didn't really explain everything I was after but it did help me to decide to use local variables inside functions to cut down on memory use.

One of the things that confused me was, according to how I read that paragraph, if I declared a variable as GLOBAL inside a function, this should have the same effect as if I declared the variable outside the function using DIM. This doesn't appear to be the case, hence, my post.

It should be the case, but SciTE is trying to keep you in "good" programming practices. I believe the script will work if you run it outside of SciTE. I would have to test that theory, but I dont have AutoIt on this computer. I will test when I get home if you dont.

I hope that helps more. I figured you had read the section from your post it seemed that way. I posted it more as a reference. I have a tendancy to lose my train of thought as I am trying to type everything I am thinking in.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Thanks JS, I know how you feel. I stare at code and just go blank sometimes. I know it's time to get up and get a caffeine fix then :-)

I'm sure its SciTE as well. I don't mind and it's probably a good thing but it tends to confuse newbies like me when we read the help file and expect things to happen but they don't.

I have run the script outside of SciTE and it appears to work without a problem, however, in keeping with good programming practices, I'll modify the way I was going to do things so that SciTE likes it as well.

Cheers

Link to comment
Share on other sites

BTW, if I do declare a local variable inside a function, do I really need to use the keyword LOCAL, or can I leave it out because AutoIt automatically assigns it local scope?

EDIT: disregard - SciTE kicks up a fuss if I don't anyhow.

Edited by PartyPooper
Link to comment
Share on other sites

Upon using MustDeclareVars, then you must declare first.

With MustDeclareVars off:

You cannot declare any variable without using Global, Dim or Local, so the answer is No. You can however Assign without declaring. Variables assigned within a function behave as if declared with Dim. So variaibles within functions should be declared Local unless you want a Dim effect to happen. Any variables used without declaring and without assigning first will stop the script with an AutoIt error.

Scite does not create a fuss as it is Au3Check that gives warning through Scite's output. Au3Check or AutoIt can separately returns errors to Scites output, yet alone other errors that come from CompileAu3... Where the errors come from can be important to fixing the errors.

I hope I have complied with the helpfiles teachings and given a another view to help understanding

Link to comment
Share on other sites

Thanks MHz. I'll read up some more on Assign but when I first looked at it, I presumed it was the equivalent as using Global or Local with an equals sign, eg:

Assign ( $var1 , 10, 1 )

Assign ( $var2 , 10, 2 )

same as

Local $var1 = 10

Global $var2 = 10

Cheers

Link to comment
Share on other sites

I was not refering previously to the Assign() function, but that is another method.

To assign a variable without using MustDeclareVars,1 is as such:

$var1 = 10

No declaration needed before use. (within functions, acts like Dimming the variable)

To declare:

Global $var1

The variable has not been assigned a value yet, but it has been declared for use.

You can declare and assign in one line:

Global $var1 = 10
Edited by MHz
Link to comment
Share on other sites

I had one of my scripts that ran and ran in a loop calling functions

However, I had a variable in the main body ($Var1) of the script, but I called a function and passed a variable of the same name. Whatever($var1)

Func Whatever($var1)

stuff $Var1 etc, etc.

Endfunc

The result was a memory leak. What I should have done was give it a different name in the function, which I did figure out in the end and this fixed my problem. Whatever($var1)

Func Whatever($DiffName)

stuff $DiffName etc, etc.

Endfunc

Maybe everyone knows this already but I just thought I should share, hope that makes sense :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...