Jump to content

Recursion and Return


 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...