Sign in to follow this  
Followers 0
Lakes

Global And Local Variables

6 posts in this topic

If I make the variable Global then I don`t need to pass the $Pen variable to the Function?

Global $Pen = 0

Func _IncCol()
    $Pen = $Pen + 1
    If $Pen >5 then 
        $Pen = 0
    EndIF
EndFunc

If the variable is local then I need to pass that variable to the function and the result is passed back?

_IncCol($Pen);Pass Var to Function

Func _IncCol($Pen)
    $Pen = $Pen + 1
    If $Pen >5 then 
        $Pen = 0
    EndIF
EndFunc

Thanks!


2015 - Still no flying cars, instead blankets with sleeves.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

HI,

what is the question?

If IncCol(5) = 0 Then 
MsgBox(0,"Bigger") 
Else
MsgBox(0,"Smaller") 
Endif

Func _IncCol($Pen)
    $Pen += 1
    If $Pen >5 then 
        $Pen = 0
    EndIF
       return $Pen 
EndFunc

So long,

Mega

Edited by th.meger

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

Share this post


Link to post
Share on other sites

HI,

what is the question?

If IncCol(5) = 0 Then 
MsgBox(0,"Bigger") 
Else
MsgBox(0,"Smaller") 
Endif

Func _IncCol($Pen)
    $Pen += 1
    If $Pen >5 then 
        $Pen = 0
    EndIF
       return $Pen 
EndFunc

So long,

Mega

The question is, am I correct in the way that local and globel variables are used, If you need to use the same variable in a function.

I guess I`m a bit hazy about the use of passing variables to functions here... :think:


2015 - Still no flying cars, instead blankets with sleeves.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The question is, am I correct in the way that local and globel variables are used, If you need to use the same variable in a function.

I guess I`m a bit hazy about the use of passing variables to functions here... :think:

Read the help file at LOCAL. There is a detailed discussion of Global/Local and using the variables in functions. Perhaps what you are looking for is about the variables passed in the actual function call? If the variable is declared outside any function, you can refer to it inside a function without passing it to the function. If the value is passed to the function, it should have a unique name inside the fuction (assigned when you declared the function).

You can tweak the following demo script to try out various combinations of variables declared outside/inside a function and globaly/localy. There are too many different variations on the theme to describe them all here:

;Test Global/Local variables
Global $G1 = "One"
$G2 = "Two"

_UseGlobal()

_UseLocal($G2)

MsgBox(64, "Results", "After _UseLocal function: " & @CRLF & _
        "$G1 = " & $G1 & @CRLF & _
        "$G2 = " & $G2)

_ChangeGlobal()

MsgBox(64, "Results", "After _ChangeGlobal() function: " & @CRLF & _
        "$G1 = " & $G1 & @CRLF & _
        "$G2 = " & $G2)


; Local Functions

Func _UseGlobal()
    MsgBox(64, "Global", "Global $G1 = " & $G1 & @CRLF & _
            "Global $G2 = " & $G2)
EndFunc  ;==>_UseGlobal

Func _UseLocal($sVar)
    Local $G1 = "Three", $G2 = "Four"
    MsgBox(64, "Local", "Function was passed $sVar = " & $sVar & @CRLF & _
            "Local $G1 = " & $G1 & @CRLF & _
            "Local $G2 = " & $G2)
EndFunc  ;==>_UseLocal

Func _ChangeGlobal()
    $G1 = "Three"
    $G2 = "Four"
    MsgBox(64, "Inside _ChangeGlobal() Function", "$G1 = " & $G1 & @CRLF & _
            "$G2 = " & $G2)
EndFunc  ;==>_ChangeGlobal

Hope that helps... :(

Edit: Tweaked malformed MsgBox(), added changing the global.

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If you want a function to change a variable, you either need to use a global variable or pass that variable to a function as ByRef.

Func swap(ByRef $a, ByRef $b)
  Local $temp
  $temp = $a
  $a = $b
  $b = $temp
EndFunc

Otherwise, the parameters are not modified.

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

If you want a function to change a variable, you either need to use a global variable or pass that variable to a function as ByRef.

Func swap(ByRef $a, ByRef $b)
  Local $temp
  $temp = $a
  $a = $b
  $b = $temp
EndFunc

Otherwise, the parameters are not modified.

Thanks Guys, much clearer now. :think:

2015 - Still no flying cars, instead blankets with sleeves.

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