Jump to content

Quick and Easy Global & Local question


Recommended Posts

I have a really easy question about the visibility of local and global variables. I would run the code at home but I'm at work and can't install Autoscript. Don't worry if my syntax is off, i'm more worried about the concept.

Code Snippit 1:

Global $x

First()

Function First()

send($x)

Second()

EndFunc

Function Second()

Send($x+1)

EndFunc

Code Snippit 2:

First()

Function First()

Local $x

Send($x)

Second()

EndFunc

Function Second()

Send($x+1)

EndFunc

Will these two pieces of code have the same output or does the second function not see $x as declared? Would Dim be better to use in any case here and why? Thanks so much for your time!

Nate

Edited by iamnaeth
Link to comment
Share on other sites

Hi,

to cut a long story short:

Global: Anywhere in the script

Local: In a function.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Well I didn't know if it was recursive and maybe I'm missing something. The second code snippet, the second function IS in the first function, so does that mean it can see $x?

Hi,

to cut a long story short:

Global: Anywhere in the script

Local: In a function.

So long,

Mega

Link to comment
Share on other sites

Hi,

maybe this helps:

Global $test = "I am global"

Func _a()
    Local $test = "I am local"
    Local $local = 10
    MsgBox(0,"Local", $test & @CR & $local)
EndFunc

_a()
MsgBox(0,"1",$test)
;MsgBox(0,"2", $local) <-- would not work!

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I have a really easy question about the visibility of local and global variables. I would run the code at home but I'm at work and can't install Autoscript. Don't worry if my syntax is off, i'm more worried about the concept.

Will these two pieces of code have the same output or does the second function not see $x as declared? Would Dim be better to use in any case here and why? Thanks so much for your time!

Nate

The DIM statement creates either global or local depending on where it's used. As a general principle, you should avoid using global variables inside functions as much as possible. Pass the data as parameters or as ByRef. This is not a strict requirement, just a best practice guideline.

Better would be:

Global $x

First($x)

Func First($Data)
     send($Data)
     Second($Data)
EndFunc

Func Second($Data)
     Send($Data + 1)
EndFunc

Declaring the input parameter variables in the Func() line automatically declares them locally to that function, so you can use the same variable name in multiple functions.

:lmao:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Yeah, I thought about the passing the variable but I was just trying to save myself extra typing and be lazy! :ph34r: I still don't have a straight answer? In the second code snippet, will the line Send($x+1) result in an error?

In your code snipit 2, since $x is not declared globaly or localy for Second(), that function should throw an error for using $x before it was declared. The $x in First() doesn't count because it's local.

Lazy coding won't hurt anything while your script is small and simple. But if you make larger more complicated scripts in the future, you may seriously regret the habits you develop (I sure did!).

:lmao:

Edit: Aw, nice doggy beat me to it! :-)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...