FoxCreek
Below is a program that calls two recursive functions.
With the first one, I am able to watch as the call stack is built with each instance of the function one by one, and then am able to watch as each instance of the function is removed from the call stack, one by one.
With the second function, I can watch the call stack grow as each instance of the function is added, but after the last instance of the function is added, and the call stack pointer is at the top of the stack, that function then returns, and it seems as if that instance of the call stack immediately returns to the next line in the program, after the line that initially called the function. It seems as if all of the parent instances of the function on the call stack are skipped.
I know this is not what really happens, because the final value after the functions return to the main part of the program is correct.
However, I would like to be able to use a breakpoint or a suspend, so that after the second function is called, as each instance of the call stack have all been added, and then start returning to the parent instances, I can stop the program each time and know that those instances are actually being removed one at a time, and not being cast off into memory as a leak.
Any ideas All of my efforts with the second function have not been successful. Struggling with this led me to creating the first function, in which I was able to successfully watch the recursion step by step. However, I really would like to verify the same thing on the second function.
Thanks.
*------------------------------------------------------------------------------
*- Program Name..: recursiveFun.prg
*------------------------------------------------------------------------------
*- Description: Visual Foxpro Recursive Factorial functions
*------------------------------------------------------------------------------
*- Synopsis...: This program demonstrate two different versions
*- of recursive functions that both determine the factorial
*- value for a number. In this case, I've used 10
*- for simplicity.
*-
*- Source: http://fox.wikis.com/wc.dll Wiki~RecursionInVFP~VFP
*-
*------------------------------------------------------------------------------
Private ll_Cntr
PRIVATE ll_funcCntr
PRIVATE ll_stuff
CLEAR
ll_Cntr = 10
ll_funcCntr = 0
ll_stuff = 0
"Beginning program!"
fact = Factorial1(ll_Cntr)
STR(ll_Cntr) + "! = " + STR(fact)
*!* fact = 0
*!* fact = recFun(ll_Cntr)
*!* STR(ll_Cntr) + "! = " + STR(fact)
"End of program!"
FUNCTION Factorial1
LPARAMETER n
IF (n < 1)
IF ll_funcCntr < 10 && track function call instances
ll_funcCntr = ll_funcCntr + 1
"In Factorial1 instace " + STR(ll_funcCntr)
ENDIF
ll_stuff = ll_stuff + 1
ELSE
IF ll_funcCntr < 10 && track function call instances
ll_funcCntr = ll_funcCntr + 1
"In Factorial1 instace " + STR(ll_funcCntr)
ENDIF
&& recursive call
ll_stuff = ll_stuff + n*Factorial1(n-1)
ENDIF && program returns here after each recursive call
IF (ll_funcCntr > 0) && track function call cleanup
"Performing Cleanup on function instance " + ;
STR(ll_funcCntr)
ll_funcCntr = ll_funcCntr - 1
ENDIF
RETURN ll_stuff && returns to next line after function call
&& in the main code above
**-- End Function
FUNCTION recFun
LPARAMETERS n
&& After the last recursive call, does the instance
&& of the function on the top of the call stack
&& return to the line after 'fact = recFun(ll_Cntr)'
&&
&& Or does it return to the next parent instance of the
&& function on the call stack until the last instance
&& of the function has returned to the calling command
&&
&& Is there a way I can stop the program in this function
&& after each instance of the function on the call stack
&& returns
RETURN IIF(n>1,n*recFUN(n-1),1)
**-- End Function