Jump to content

Declaring Var in Func


Zepx
 Share

Recommended Posts

Hi all,

This has been bothering me.. well basically it's just my style. I like declaring variables in functions and then read them in another function ie.

1()
MsgBox(0, "", $var1)


Func 1
Global $var1 = "Something"

It works but I get warning. Something wrong?

Link to comment
Share on other sites

Heres An Example Hope It Helps! :) For More Questions Add Me On MSN At 'maxell225@hotmail.com'

Call ( "Variable" ) ; 'Calls A Func Named Variable'

Func Variable () ; 'Sets Func Named Variable'
Global $Variable = "Variable Test" ; 'Global Lets You Sets Your Variable So That It Can Be Used Any Where In The Script'
EndFunc ; 'Ends The Func'

Sleep (1000) ; 'Pauses The Script For 1000 Milla Seconds (1 Second)'
Call ( "_Test" ) ; 'Calls A Func Named _Test'

Func _Test () ; 'Sets A Func Named _Test'
MsgBox (0, "Test", $Variable ,0) ; 'Makes A MsgBox With The Title 'Test' And The Text Of $Variable'
Exit ; 'Exits The Program/Script'
EndFunc ; 'Ends The Func'

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Thanks john, I still get warning anyway, don't know why, but it is possible for the output.

Declaring many empty variables at the start is a bit frustrating for me, but I'll manage it somehow.

Edited by Zepx
Link to comment
Share on other sites

Isn't the warning your first indicator that maybe you have bad style? If you need to access variables from other functions, then you need to learn about function parameters, function return values and byref parameters. Using global variables is a bad enough habit, but using global variables which are declared inside functions is 10x worse. I guarantee you'll eventually end up with a branch of code that fails to call the function which will in turn lead to AutoIt aborting when you try to use a variable not declared.

Link to comment
Share on other sites

Is global vars in autoit bad? Just wondering because in C++ a tutorials says it is never good to have Global vars as other programs at the same time can modify that address.

I'll try to get rid of the habbit of using too many global vars.

Link to comment
Share on other sites

Is global vars in autoit bad? Just wondering because in C++ a tutorials says it is never good to have Global vars as other programs at the same time can modify that address.

I'll try to get rid of the habbit of using too many global vars.

Using too many global variables in any language is bad. If you use global variables with the same names as variables in included header files or libraries, etc, you can start encountering serious conflicts. In addition, using the same variable by so many functions at the same time can cause unforseen errors. All in all, it is better just to use references to variables that are needed by using the ByRef statement when declaring the function's variable parameters.

In other languages, you would simply pass a pointer to the variable as the function parameter. Passing a pointer is much faster than passing the actual data. By using ByVal, the default, instead of ByRef, you will slow the program down because the program has to stop, allocate memory, and then copy all the varaibles' data into separate memory addresses. This is why using parameters that are ByRef is better. It is faster and more effiicient. It also means you don't have to use global variables and can easily pass data from one function to another. Plus, some functions may have local scope variables that get deleted from memory once the function finishes. Global variables always retain their values and are always in memory from the time they are declared to the time they are deallocated. In Autoit, deallocating global scope variables is not possible.

The point is, try to use as few global variables as possible to keep the program's data flow coherent and clean.

- The Kandie Man ;-)

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Thanks kandie! That was a very clear explanation.

Btw guyz my problem is solved, basically I need to change AutoItSetOption("MustDeclareVars", 1) to avoid getting errors. But I won't do it since it's a not good habbit.

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...