Sign in to follow this  
Followers 0
themax90

Local/Global Variable Tutorial

4 posts in this topic

#1 ·  Posted (edited)

As I sat down to talk with someone the other day it occured to me by the questions they were asking that they did not know the difference or benefit of Local/Global variables. So I wrote this tutorial. I would like to know how it is, being it's my first one.

Experimenting with Localization

Global $X1 = 5 ; Declares a variable in the global scope.
Local $X2 = 10 ; Declares a variable in the local scope.
_MsgBox() ; Calls a function. (Function Alt-Script)
MsgBox(0, $X1, $X2) ; Calls a function. (Native) (Main Script)
Func _MsgBox()
    ;Local $X2 = 15 ; Declares a variable in the local scope of the function.
    MsgBox(0, $X1, $X2)
EndFuncoÝ÷ Ø2¢ë{^*.§ʫº{ąº­·
&zËæèÅëì¢ØZ­äâ¶W
Edited by AutoIt Smith

Share this post


Link to post
Share on other sites



Experimenting with Globalization

Global $Var1 = 0 ; Declares a variable in the global scope.
Local $Var2 = 1 ; Decalres a variable int he local scope.
; Experiment with the line above.  Change Local to Global and see what happens.
Vars()
MsgBox(0, $Var1, $Var2)
Func Vars()
    $Var1 = 2
    $Var2 = 3
EndFuncoÝ÷ ØÚ-Ç­«péò¢êî+mᥡ¶¥½ªâi¹^Ó~j½©àzËhÛbëaxºj[Ú®&í7éV«ÚËZÊÆ­×}÷ß}Ç£¯z¸Ê¬zÝ7éV«Ù«Ú+pY©àxWúÞzÚÆ«éÖÞ~Þrí{¢h¶¢w%ºÈ¨ú®¢×®Ë«xJë¯MúUªö{azg¬±¨npY[{{ajÜÈƲm쨺gÖ®¶­sdvÆö&Âb33cµf#Ò²FV6Æ&W2f&&ÆRâFRvÆö&Â66÷Rà¤Æö6Âb33cµf#"Ò²FV6Ç&W2f&&ÆRçBRÆö6Â66÷RࣲWW&ÖVçBvFFRÆæR&÷fRâ6ævRÆö6ÂFòvÆö&ÂæB6VRvBVç2à¥f'2¤×6t&÷Âb33cµf#Âb33cµf#"¤gVæ2f'2 b33cµf#Ò ¢Æö6Âb33cµf#"Ò0¤VæDgVæ
?

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Correct, Thank you, It's been a late night.

Share this post


Link to post
Share on other sites

a local variable changes to the specific function or in some cases main script. Please consider this example:

Why are you declaring a variable as Local outside of a function? :whistle:

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
Sign in to follow this  
Followers 0