Sign in to follow this  
Followers 0
Krzysztof

Global statement does not work

5 posts in this topic

Hello everyone. I am new here.

I am just learning AutoIt from video-course. I wrote an example of function (taken from that course), please see the code below. It works fine for the tutor, but not for me. The problem is with global statement, because every time I receive following information

warning: $Sum: possibly used before declaration

I can't find the solution, can You? I know the warning is expected with no "global", but as You see I used it to have the access to $Sum variable in entire code.

$Box1 = InputBox("Addition", "First number is: ", "")
$Box2 = InputBox("Addition", "Second numer 2 is: ", "")
Addition($Box1, $Box2)
MsgBox(0, "Info", "The sum is: " & $Sum)

Func Addition($Number1, $Number2)
    Global $Sum = $Number1 + $Number2
    Return $Sum
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This is more correct.

Global $Box1 = InputBox("Addition", "First number is: ", "")
Global $Box2 = InputBox("Addition", "Second numer 2 is: ", "")
Global $Sum = Addition($Box1, $Box2) ; Declare and assign a value to $Sum
MsgBox(0, "Info", "The sum is: " & $Sum)

Func Addition($Number1, $Number2)
    Local $Sum = $Number1 + $Number2 ; Use Local instead of Global when declaring variables within a function.
    Return $Sum
EndFunc

;

I prefer to use the above method which uses a return value, but you can also modify global variables within a function as shown below:

Global $Box1 = InputBox("Addition", "First number is: ", "")
Global $Box2 = InputBox("Addition", "Second numer 2 is: ", "")
Global $Sum ; Declare $Sum
Addition($Box1, $Box2)
MsgBox(0, "Info", "The sum is: " & $Sum)

; This function does not return a value 
Func Addition($Number1, $Number2)
    $Sum = $Number1 + $Number2 ; Modify the global variable
EndFunc

;

Please note: In the first example there are two instances of the variable $Sum. These two variables do not conflict because the one declared local (within the function) ceases to exist after the function has terminated. Once declared, the local variable takes precedence over the global variable when inside the function. Some of this might not make sense at first, but there are a lot of discussions about Local vs Global variables. I suggest you use the forum search feature and read up on the subject.

Edited by czardas

Share this post


Link to post
Share on other sites

Thank You for the answer and explanations. But have You any idea why the very first code works properly in the case of video-course? It should not work (not compile), but it works. It is strange, isn't it?

Share this post


Link to post
Share on other sites

Krzysztof,

Welcome to the AutoIt forums. :)

The warning you see is produced by Au3Check, one of the utilities that are added to the fullSciTE4AutoIt3 editor package to help us code in AutoIt. If you add #AutoIt3Wrapper_Run_AU3Check=n you will see that the code runs without problem. ;)

It runs because the $Sum variable is declared Global inside the function before it is used in the MsgBox line - so it does exist when required. But this is not good coding practice - I suggest reading the Variables - using Global, Local and ByRef tutorial in the Wiki to get a better handle on how variable scoping works in AutoIt. And never be afraid to post asking for clarification - that is why we are here. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank You for the explanation. I am not a coder (I am trying to be a tester), so I don't "feel" that coding problems. But Your explanations are clear and sure they will help me.

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