Ktulu789

Understanding Variables in Functions

9 posts in this topic

#1 ·  Posted

I was trying to search for this specific topic without luck. I hope this is not a repost.

I have a script with two functions, the first creates a Global array, then it calls the second function.

The second function does something and if it finds what I'm looking for, it would show something from the Global array...

It is as if the array never existed for the second function!

GLOBAL $Arr[5]=[1,2,3,4,5];This wont be seen from _Check()
GLOBAL $Count

Start ()

Func _Start()
   For $Count=0 To UBound($Arr);$Count wont be seen from _Check either
      _Check()
   Next
EndFunc

Func _Check()
    If $Arr[$Count]==3 Then MsgBox(0,"Hey!",$Count);$Count says it's out of bounds
EndFunc

This is a simple example, I quickly wrote here, sorry if there's any typo :P


AutoIt is a blessing, I don't know how I was able to use my computer before [Auto]It :-S

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm not exactly sure what you're asking, but there are a few problems with your example:

GLOBAL $Arr[5]=[1,2,3,4,5];This wont be seen from _Check()
GLOBAL $Count

_Start();<======MODIFIED

Func _Start()
   For $Count=0 To UBound($Arr)-1;<======MODIFIED
      _Check()
   Next
EndFunc

Func _Check()
    If $Arr[$Count]==3 Then MsgBox(0,"Hey!",$Count);$Count says it's out of bounds
EndFunc

You might have been going out of bounds because in your for loop you were going one index too far.

To make things clearer as well, if you declare a variable as Global then it is visible to anything in the script, in all functions. So your $Arr and $Count will be visible and accessible everywhere.

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Anthony! Thanks for the quick response!

I warned there could be typos :P I added the "_" in the end

As for the -1, you are right. but at least the _Check function should run 4 times before failing, therefore showing the message with the counter. But that's not what's happening.

I get a message saying "Hey!","" but  the text should be the number in $Count, right?

GLOBAL $Arr[5]=[1,2,3,4,5];This wont be seen from _Check()
GLOBAL $Count

_Start()

Func _Start()
   For $Count=0 To UBound($Arr)
      _Check()
   Next
EndFunc

Func _Check()
    If $Arr[$Count]==1 Then MsgBox(0,"Hey!",$Count);The message has Title "Hey!" and text ""
EndFunc;Modifyed the 3 for a 1

I have run this example and noticed that even without the "Ubound-1" it wont fail.

Edited by Ktulu789

AutoIt is a blessing, I don't know how I was able to use my computer before [Auto]It :-S

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oh, I understand your question now. 

If I'm not mistaken, the counter in a For..Next loop is always Local. This means it is overriding your global variable. If you wanted to use it in another function, you can pass it into the function like so:

GLOBAL $Arr[5]=[1,2,3,4,5]

_Start()

Func _Start()
   For $Count=0 To UBound($Arr)-1
      _Check($Count)
   Next
EndFunc

Func _Check($Count)
    If $Arr[$Count]==3 Then MsgBox(0,"Hey!",$Count)
EndFunc; Changed 1 back to 3

EDIT: OR you could use a separate variable for counting, like so:

GLOBAL $Arr[5]=[1,2,3,4,5];This wont be seen from _Check()
GLOBAL $Count = 0

_Start()

Func _Start()
   For $i=0 To UBound($Arr)-1
      _Check()
      $Count +=1
   Next
EndFunc

Func _Check()
    If $Arr[$Count]==3 Then MsgBox(0,"Hey!",$Count);The message has Title "Hey!" and text ""
EndFunc;Modifyed the 3 for a 1

 

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#5 ·  Posted

That's what I did! I passed the variables as parameters!
But I didn't understand why this happened!!!

O.O

Now everything is clear! Thanks a bunch!


AutoIt is a blessing, I don't know how I was able to use my computer before [Auto]It :-S

Share this post


Link to post
Share on other sites

#6 ·  Posted

Even more:

For $Count=0 To 10
   For $Count=0 To 3
     code...
   Next
Next

These two variables ARE just one, right? Hence they will interfere with each other?


AutoIt is a blessing, I don't know how I was able to use my computer before [Auto]It :-S

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

No they will not interfere with each other, although it is still bad practice to code that way. If you printed count from within the innermost loop, it will only output the count variable in that context. If you print it outside the loop, it will print in that local context. At least this is how it seems from what I've tested.

Actually scratch that, I think they do interfere because it is an infinite loop. So yes, since you defined it local in the first for loop, it stays the same variable in any nested loops.

Edited by anthonyjr2
modification

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

For $Count=0 To 10
   For $Count=0 To 3
     MsgBox(0,"In",$Count)
   Next
  MsgBox(0,"Out",$Count)
Next

This first Goes:

In 0

In 1

In 2

In 3

Then

Out 4

Never Out 0 or Out 1, etc

And Out is always 4 so there is an infinite loop.

Edited by Ktulu789

AutoIt is a blessing, I don't know how I was able to use my computer before [Auto]It :-S

Share this post


Link to post
Share on other sites

#9 ·  Posted

Try it this way instead, pass the variable from the first function to the second and don't reuse variable names like that, it's bad programming.

GLOBAL $Arr[5]=[1,2,3,4,5];This wont be seen from _Check()
; GLOBAL $Count

_Start()

Func _Start()
   For $Count=0 To UBound($Arr) - 1
      _Check($Count)
   Next
EndFunc

Func _Check($CountIn)
    If $Arr[$CountIn]=1 Then MsgBox(0,"Hey!",$CountIn);The message has Title "Hey!" and text ""
EndFunc;Modifyed the 3 for a 1

 


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

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