kylomas Posted August 1, 2015 Share Posted August 1, 2015 This is one of those 2:30AM questions.I am expecting this code to return 'Yes' but it is returning 0.local $var1 = '' ConsoleWrite(test1() & @CRLF) func test1() ConsoleWrite('At top of function' & @CRLF) if $var1 = '' Then $var1 = 'yes' ConsoleWrite('at = condition... $var1 = ' & $var1 & @CRLF) test1() Else ConsoleWrite('returning from else... $var1 = ' & $var1 & @CRLF) return $var1 EndIf endfuncWhen I run it like this it works...local $var1 = '' ConsoleWrite(test1() & @CRLF) func test1() ConsoleWrite('At top of function' & @CRLF) if $var1 = '' Then $var1 = 'yes' ConsoleWrite('at = condition... $var1 = ' & $var1 & @CRLF) return test1() Else ConsoleWrite('returning from else... $var1 = ' & $var1 & @CRLF) return $var1 EndIf endfuncThe only difference is that I added a return to the recursive call to the function. My original thought was that I was not getting the value of $var1 for the recursive call but I don't see how as this is a global variable.Hmmm...now 3:04AMkylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
JohnOne Posted August 1, 2015 Share Posted August 1, 2015 Return from test1() else condition is only returning to test1() if condition, which is returning nothing.local $var1 = '' ConsoleWrite(test1() & @CRLF) func test1() ConsoleWrite('At top of function' & @CRLF) if $var1 = '' Then $var1 = 'yes' ConsoleWrite('at = condition... $var1 = ' & $var1 & @CRLF) test1() Else ConsoleWrite('returning from else... $var1 = ' & $var1 & @CRLF) return $var1 EndIf return $var1 endfunc AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted August 1, 2015 Author Share Posted August 1, 2015 J1,Gonna have a beer and go to bed. Maybe I'll understand your reply tomorrow.kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
JohnOne Posted August 1, 2015 Share Posted August 1, 2015 lol best advice anyone can ever give themselves.local $var1 = '' ConsoleWrite(test1() & @CRLF) func test1() ConsoleWrite('At top of function' & @CRLF) if $var1 = '' Then $var1 = 'yes' ConsoleWrite('at = condition... $var1 = ' & $var1 & @CRLF) test1() ; this is a recursive call, that leads to the function getting to the else condition, then it returns here and goes to end of function which returns nothing Else ConsoleWrite('returning from else... $var1 = ' & $var1 & @CRLF) return $var1 EndIf ;returning nothing endfunc AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted August 2, 2015 Author Share Posted August 2, 2015 OK, I think, but why does the second pass through test1() return 0 when $var1 = 'Yes' as shown in the consolewrite?Apparently the sleep did nothing to clear the confusion...kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 (edited) This is the "first pass" to test1()...ConsoleWrite(test1() & @CRLF)It enters the If condition because $var1 = ""Now in that If condition test1() is called a second time recursively "second pass"But now $var1 = "yes", so this time it enters the Else condition which returns $var1 (which = "yes"But it has only returned that value to the point from which it was called in the "first pass" If condition, and that is the end of "second pass"Now the first pass can continue, since it's done its job in the If condition it has nothing left to do except reach EndFunc, where it returns nothing to its caller...ConsoleWrite(test1() & @CRLF)Phew. Edited August 2, 2015 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted August 2, 2015 Share Posted August 2, 2015 The only explanation I can think of is that $var1 is not written to in the global scope until the function returns. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
kylomas Posted August 2, 2015 Author Share Posted August 2, 2015 (edited) J1, jaberwacky,But it has only returned that value to the point from which it was called finally cleared my mental fugue...I blame my recently deceased Rubik's Cube.Again, thank you for your patience.kylomas Edited August 2, 2015 by kylomas spelling Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
guinness Posted August 2, 2015 Share Posted August 2, 2015 (edited) One point to add for clarity, use the Global keyword if the variable is going to be globally accessible. In this instance the variable is only "local" to the "global (AutoIt) scope"*. It makes things a little easier to read. Anyway, the less global variables, the better I say.* I would argue it's not really global scope. Edited August 2, 2015 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 (edited) The only explanation I can think of is that $var1 is not written to in the global scope until the function returns.But it is though, here "$var1 = 'yes'" it's the first and only thing the function does.besides a couple of ConsoleWrite.kylomas you should really try to understand this, I can see how it might confuse you, but it is very basic requirement to understand the flow of your code.EDIT: let me try again.func test1() ConsoleWrite('At top of function' & @CRLF) if $var1 = '' Then $var1 = 'yes' ConsoleWrite('at = condition... $var1 = ' & $var1 & @CRLF) $return = test1() ; Else ConsoleWrite('returning from else... $var1 = ' & $var1 & @CRLF) return $var1 ; This return only returns to $return variable. EndIf ; the return to Consolewrite comes from here ;as you should see, it returns nothing endfunc Edited August 2, 2015 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Danyfirex Posted August 2, 2015 Share Posted August 2, 2015 To avoid the global variable. you could use Static variables. Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 The variable being global is beside the point, the value being printed is a return from a function that returns nothing. Danyfirex 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 Am I being trolled here? AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted August 2, 2015 Share Posted August 2, 2015 Looks like, J1!Also a Static for handling recursion is a joke. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jaberwacky Posted August 2, 2015 Share Posted August 2, 2015 (edited) I'd still say that maybe a local copy of $var1 is made and stays local until the func returns? Edit: for the record, I'm not trolling. Edited August 2, 2015 by jaberwacky Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 The global variable " Local $var1 = ''" is set to 'yes', it's just never used anywhere outside of the function. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted August 2, 2015 Author Share Posted August 2, 2015 J1,I get it, see post #8... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
JohnOne Posted August 2, 2015 Share Posted August 2, 2015 Ok, my apologies I never seen your edit. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted August 2, 2015 Author Share Posted August 2, 2015 No apology necessary, thanks for taking the time to help clear the fog... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
guinness Posted August 3, 2015 Share Posted August 3, 2015 Also a Static for handling recursion is a joke.Why you say that? UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
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