Jump to content

Can a recursive function be contained within a non-recursive function?


Recommended Posts

Hi everyone. I am new to this forum, but not new to AutoIT. I have written two functions which work together.

One of them, "Ben", is recursive and makes a call to itself. The other, "Jerry", is not recursive but does make a single call to Ben, among other things.

The problem I have is that for all intents and purposes, Jerry is all I need. He calls on Ben and returns the values I want. So is there a way to sort of "package" Ben inside of Jerry? So that I don't need to define both functions every time I use Jerry?

To summarize:

Func Ben(some parameters)
blah blah
Ben(some parameters)
EndFunc

Func Jerry(some parameters)
some code
more code
Ben(some parameters)
more code
return some value
EndFunc
Link to comment
Share on other sites

You could but it would get ugly. You would have to add a flag parameter to "Jerry" that triggers a Switch statement within the function.

Func Jerry($param1, $param2, $param3, $recurse)
If $resurse Then
Jerry($param1,$param2,$param3,TRUE)
Else
;Non-recursive stuff
EndIf
EndFunc
Link to comment
Share on other sites

Yes. I actually do have that as part of my code for Ben, which is why I created Jerry to envelope him - so that the end user doesn't see or know about the flag parameter.

Maybe just set a global variable: Global $recurse = true

Link to comment
Share on other sites

A local variable would work fine.

code
code
Jerry(14,34)
code

Func Jerry($var1, $var2, $ben = False)
    If Not $ben Then
         Jerry's code
    Else
         Ben's code (which may or may not implement $var1 and $var2)
         Jerry(0, 0, True)
         When it's finally time to exit Ben, do your final stuff and return
    Endif
EndFunc
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...