onefish

Noob question regarding functions and global variables

7 posts in this topic

I was reading BEST CODING PRACTICES earlier and realized that I had more or less broke most of them. I've just been rearranging this script I'm working on and trying to get a bit closer to "better coding practices" (best is a way off yet :-). I hadn't considered memory used by variables as much of a concern and instead structured my script in favor of lower CPU. I'm still trying to get a full understanding but from what I have read, local variables within a function are released from memory once the script moves outside the function, correct?

That works for most of the error checking I need to do but there are 2x variables that require a bit to build (CPU wise) but they are needed to check for errors before actually using them in the script. I read in the Best Coding Practices that I shouldn't declare global variables from inside a function and I don't want to have to rebuild these couple variables again once the script moves passed error checking.

What is the correct way to manage this? If I declare the global variable with value "" before the function start and then, inside the function, build the correct string and re-declare the global variable does the re-declared string value stick and become available for the next function?

Thanks for your help!

Share this post


Link to post
Share on other sites



You're confusing the Local and Global variables. A global variable declared inside a function is still global outside of it.

Global $OutsideVariable1 = "Declared", $OutsideVariable2 = "Declared"
ConsoleWrite("$OutsideVariable1 = " & $OutsideVariable1 & @CRLF)
ConsoleWrite("$OutsideVariable2 = " & $OutsideVariable2 & @CRLF)
Example()
ConsoleWrite("$OutsideVariable1 = " & $OutsideVariable1 & @CRLF)
ConsoleWrite("$OutsideVariable2 = " & $OutsideVariable2 & @CRLF)
;~ ConsoleWrite("$InsideVariable = " & $InsideVariable & @CRLF) ; causes an error if uncommented
Func Example()
    Local $InsideVariable = "Inside"
    $OutsideVariable1 = "Redeclared" ; changes the value of the variable
    Global $OutsideVariable2 ; ignored because it's already declared as a global
    ConsoleWrite("$InsideVariable = " & $InsideVariable & @CRLF)
EndFunc   ;==>Example

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks BrewManNH, I was just interpreting these statements from the best practice guide, perhaps I have misunderstood them:

"Always declare your global variables in the global scope, not in the functions."
"Declaring Global variables in a Function is never a good idea:"

So, if I understand you right I can declare $g_sSomeLongString = "", before the script function and then re-declare $g_sSomeLongString="abcdefghijklmnopqrstuvwxyz" inside the function and the alphabet will be the value of the variable next time it is called up by another function (in the same script of course)?

I don't need the variable before it's tested for errors anyway so there's technically no benefit to declaring it before the error checking function but I was thinking that there must be a reason why the tutorial said not declare global variables inside a function.

Share this post


Link to post
Share on other sites

IF it's a global variable it should never be declared inside a function because you might reference it in the script before you call the function. If you run Au3Check on your script you might get a warning that the global variable isn't declared too.

Here's a slight modification of the previous script, with the declaration line for $OutsideVariable2 moved inside the function

Global $OutsideVariable1 = "Declared"
ConsoleWrite("$OutsideVariable1 = " & $OutsideVariable1 & @CRLF)
Example()
ConsoleWrite("$OutsideVariable2 = " & $OutsideVariable2 & @CRLF)
ConsoleWrite("$OutsideVariable1 = " & $OutsideVariable1 & @CRLF)
ConsoleWrite("$OutsideVariable2 = " & $OutsideVariable2 & @CRLF)
;~ ConsoleWrite("$InsideVariable = " & $InsideVariable & @CRLF) ; causes an error if uncommented
Func Example()
    Local $InsideVariable = "Inside"
    $OutsideVariable1 = "Redeclared" ; changes the value of the variable
    Global $OutsideVariable2 = "declared" ; ignored because it's already declared as a global
    ConsoleWrite("$InsideVariable = " & $InsideVariable & @CRLF)
EndFunc   ;==>Example

While it will work ok doing it that way, you shouldn't declare Globals inside functions just because someone else may be looking at your code and not realize where you declared $OutsideVariable2 and they'd have to search the script to find it. If you declare all your Global variables at the top of the script, it makes it easier to follow.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Choose your wording with care. A global function should seldom need to be redeclared. What you are doing here is reassigning new data to an already existing global variable. Declaring a global variable inside a function is a superfluous action.

2 people like this

Share this post


Link to post
Share on other sites

Ahh, yes you are correct czardas, reassigning new data.

Share this post


Link to post
Share on other sites

You also can also use ByRef.to passing variables:

Quote

The ByRef keyword indicates that the parameter should be treated as a reference to the original. By default the parameter is copied into a new variable but ByRef links the new variable to the original. Note that not only a named variable can be passed for a ByRef parameter - unnamed temporary variables, such as function return values, may be passed as ByRef parameters as well. However, a literal cannot be passed to a ByRef parameter. ByRef should be used when passing large amounts of data (such as the contents of a file) where copying all the data would impose a significant performance penalty. Another advantage is that passing a parameter ByRef when the function is intended to change the content of the parameter removes any requirement to Return the changed value as the original is directly affected.
from Helpfile (func ... endfunc)

 

Share this post


Link to post
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