Jump to content

Does calling functionA in functionA cause any long term problems?


Recommended Posts

Hi guys! Im not sure how these scripts you build with AutoIT works, that's why I came to think of if calling a function inside a function causes any problems.

For example:

Func main()
  "Do this"
  "Do that"
  "Check something"
  If @error then
     main()
  endif
  "Do 2"
  "Do 3"
End Func

Does this cause the function main() to run inside the function main and possible do this over and over and over..... How does it work?

Edited by Sniperwolf
Link to comment
Share on other sites

Ok, thanks for the reply.

To explain even further why I am doing this.

I have a main() that inside calls a lot of other different functions, but in one of those function if a condition is met I reset all the stored values then run main() to start the function from the top again.

If this isn't the way I should do it, anyone have any suggestion on a different way of doing it perhaps?

Was just thinking if the script would fail if it does this like 100 times or something, if the script needs to keep track of in which function it is in (yes sir I am having trouble explaining what I mean in a normal understandable way :) )

Then it would be inside the 99th main() inside all other previously called main(). Kind of like inside main()inside the func that called main() inside main() inside the function that called main() and so on.

Or am I just not getting how scripts work?

Edited by Sniperwolf
Link to comment
Share on other sites

This is called recursion. Check it out it is general chapter in programming.

Calculation of x factoriel is a good example.Not very wise to be used from beginners.

In your case i would suggest to run the main function in a loop

Edited by Juvigy
Link to comment
Share on other sites

If the program is going to be executed for a long time and it's expected that the condition is going to be satisfied many times you may want to rewrite the program in a way the functionalities you wish to preform more than only in the program initialization are on their on function or functions. With the script you've posted, main() will somewhere during it's execution exceed the recursion depth limit and crash, it'll also eat a lot of ram but for as long as it remains runnning. :)

Link to comment
Share on other sites

From what I hear then I should probably try and stay away from that then :) . So by using a loop does the following work?

Can I exit a loop from inside a function inside of the loop?

Func main()
    While 1
        func1()
        func2()
        CheckCondition()
        func3()
        func4()
    Wend
EndFunc

Func CheckCondition()
Checksomething
If @error then ExitLoop
EndFunc

While 1
    main()
WEnd

Or is this the way to do it?

Func main()
    While 1
        func1()
        func2()
        CheckCondition()
        If $conditionmet=1 Then ExitLoop
        func3()
        func4()
    Wend
EndFunc

Func CheckCondition()
Checksomething
    If @error then
        $conditionmet=1
    EndIf
EndFunc

While 1
    main()
WEnd

This assuming I can have loops inside loops and the exitloop only exit the current loop the script is running yes?

Link to comment
Share on other sites

Thanks for all your help guys, much appriciated, something like this will work then to avoid recursion

Func main()
    MsgBox(0,"Message","Starting main function")
    While 1
        MsgBox(0,"Message","First func inside main")
        MsgBox(0,"Message","Second func inside main")
        If CheckCondition()=1 Then ExitLoop
        MsgBox(0,"Message","Third func inside main")
        MsgBox(0,"Message","Fourth func inside main")
    Wend
EndFunc

Func CheckCondition()
    $question = MsgBox(4,"Make your choice!","Do you want the script to restart from the begginning?")
    If $question = 6 Then
        Return 1
    EndIf
EndFunc

While 1
    main()
WEnd
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...