Krzysztof Posted January 1, 2015 Posted January 1, 2015 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
czardas Posted January 1, 2015 Posted January 1, 2015 (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 January 1, 2015 by czardas operator64 ArrayWorkshop
Krzysztof Posted January 1, 2015 Author Posted January 1, 2015 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?
Moderators Melba23 Posted January 1, 2015 Moderators Posted January 1, 2015 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Krzysztof Posted January 1, 2015 Author Posted January 1, 2015 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now