Page 1 of 1

Calling a subroutine from a subroutine

PostPosted: Thu Dec 08, 2011 9:22 am
by dpayer
Besides the issue of looping, is there a technical problem of calling subroutines from within another subroutine?

I am finding it clearer to separate out all my processes into functions or subroutines and then just call them. I find I need some of those processes within a sub routine that are part of another subroutine.

David P.

PostPosted: Thu Dec 08, 2011 11:32 am
by Gaev
David P.:

I use GoSub within GoSub all the time ... sometimes nested 4 deep ... no problem at all.

Just make sure you don't start chasing your tail e.g.
Code: Select all
:John
GoSub "Mary"
Return

:Mary
GoSub "John"
Return

Nesting gosubs

PostPosted: Thu Dec 08, 2011 2:59 pm
by carlos torres
Hi dpayer

I thing that the Gaev suggestion is a good one and mandatory!

More than 4 levels of nesting meke difficult to follow the program logic.

regards

carlost

PostPosted: Fri Dec 23, 2011 6:40 am
by andybrock
I've got some code that calls gosubs recursively without any issue.

Andy

PostPosted: Fri Dec 23, 2011 10:27 am
by Gaev
Andy:

You can call subroutines recursively ... what you need to avoid is ... a never ending loop ... like the simple example in my previous post ... something like this is fine ...
Code: Select all
:Recursive
If "[myCount]" ">=" "5"
   SetVar "[myCount]" "[myCount]-1"
   GoSub "Recursive"
Else
   Return
EndiIf