kylomas

Unexpected value returned from function call

23 posts in this topic

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

endfunc

When 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

endfunc

The 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:04AM

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

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 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

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

To avoid the global variable.  you could use Static variables.

 

Saludos

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

#15 ·  Posted (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 by jaberwacky

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Also a Static for handling recursion is a joke.

Why you say that?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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