Sign in to follow this  
Followers 0
RAMzor

Recursion and Return

3 posts in this topic

#1 ·  Posted (edited)

Hello!

I have confused with recursion.

How can I return value (@error in my case) from function after some self calling?

1. Run code

2. For "Test Passed?" message answer "NO"

3. For "Communication ERROR" message answer "Retry"

4. Perform step 2 and 3 several times and then click "Cancel"

_Test()
If @error Then 
    MsgBox(16, "", "::: Communication ERROR :::")
Else
    MsgBox(0, "", "Test successfull")
EndIf

Func _Test()
    $AnswTest = MsgBox(4, "TEST", "Test Passed?")
    If $AnswTest = 7 Then ; NO (Test Failed)
        $Answ = MsgBox(21, "Error", "Communication ERROR")
        Select
            Case $Answ = 4 ;Retry
                _Test()
            Case $Answ = 2 ;Cancel
                Return SetError(1)
        EndSelect
    EndIf
    
    ConsoleWrite("Test Passed" & @LF)
    Return 
EndFunc   ;==>_Test
Edited by RAMzor

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you need to return and pass the @error after _Test returns.

_Test()
If @error Then
    MsgBox(16, "", "::: Communication ERROR :::")
Else
    MsgBox(0, "", "Test successfull")
EndIf
Func _Test()
    $AnswTest = MsgBox(4, "TEST", "Test Passed?")
    If $AnswTest = 7 Then; NO (Test Failed)
        $Answ = MsgBox(21, "Error", "Communication ERROR")
        Select
            Case $Answ = 4;Retry
                _Test()
                return SetError(@error)
            Case $Answ = 2;Cancel
                Return SetError(1)
        EndSelect
    EndIf
    ConsoleWrite("Test Passed" & @LF)
    Return
EndFunc ;==>_Test

I would avoid the recursion in this case and make the _Test func like this:

Func _Test()
    While MsgBox(4, "TEST", "Test Passed?") = 7
        If MsgBox(21, "Error", "Communication ERROR") = 2 Then Return SetError(1)
    WEnd
    ConsoleWrite("Test Passed" & @LF)
    Return
EndFunc  ;==>_Test
:) Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you very much! All works! :)

In my example I used the "aaa" for illustrative purposes only, in reality, instead, there are a lot of code. Therefore, the second example does not fit 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