Jump to content
Sign in to follow this  
Decker87

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

Recommended Posts

Decker87

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

Share this post


Link to post
Share on other sites
weaponx

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

Share this post


Link to post
Share on other sites
Decker87

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.

Share this post


Link to post
Share on other sites
weaponx

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

Share this post


Link to post
Share on other sites
crzftx

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

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  

×