Influence invoking a recursive sub on non-changing parameters?

Stellaferox

Active Member
Licensed User
Hi,

I am writing a little program which uses a sub which invokes itself in a recursive loop as a translation from python-code.

I find that one parameter (numCoins) diminishes each time the recursion takes place while the parameter itself never is changed.

How is this possible?

I attached the code.

thnx in advance

PS: the program is ment to calculate the probability for a run of at least K Heads in N times flipping a coin.
 

Attachments

  • $$$Coins Markov Serie 2.sbp
    3.8 KB · Views: 228

mjcoon

Well-Known Member
Licensed User
I find that one parameter (numCoins) diminishes each time the recursion takes place while the parameter itself never is changed.

But
B4X:
For firstTail = 1 To minHeads+1
   pr = ProbOfStreak(numCoins-firstTail)
performs the recursion with the parameter reduced by one, as you have seen.

This will carry on until an entry in ProbArray is found > 0, to terminate the recursion, but all entries are set to -1 at start-up. And not changed until after that loop completes, which in turn requires each looped recursion to complete. Catch-22!

So I'm not surprised you see what you see. What I do not understand is how it is supposed to work!

Mike.
 

Stellaferox

Active Member
Licensed User
Hi mjcoon,

Do I understand correctly that (numCoins-firstTail) is passed to the sub (and numCoins) and with this transaction diminishes numCoins?

Marc
 

mjcoon

Well-Known Member
Licensed User
Do I understand correctly that (numCoins-firstTail) is passed to the sub (and numCoins) and with this transaction diminishes numCoins?

The answer is "yes" because what is passed into the Sub is a value. Each (recursive or not) invocation of the Sub has its own distinct value of numCoins.

As a recursive invocation completes and returns to the calling invocation it also returns to that distinct value of numCoins. You can demonstrate this to yourself using a cut-down version of your program the does MsgBox calls, or by running under Debug.

(This would not apply if you used
B4X:
Sub ProbOfStreak(ByRef numCoins)
but then the call
B4X:
pr = ProbOfStreak(numCoins-firstTail)
becomes illegal (as I have just demonstrated to myself by trying it).)

Mike.
 

Stellaferox

Active Member
Licensed User
OK, thanks.

This would mean that a workaround should not use a For-Next loop with a counter that runs from A to B, subtracted from NumCoins and passed to the recursionloop but a steady decrease by 1, a certain number of times and feed that into the loop.
I am not certain whether I could use the ByRef statement.....

Marc
 

mjcoon

Well-Known Member
Licensed User
This would mean that a workaround should not use a For-Next loop with a counter that runs from A to B, subtracted from NumCoins and passed to the recursionloop but a steady decrease by 1, a certain number of times and feed that into the loop.

I'm not sure that is going to make any difference. Perhaps someone else, who understands how the whole algorithm is intended to work, can suggest an effective modification.

I am not certain whether I could use the ByRef statement.

No, I didn't intend that you should; it's not so appropriate for a recursive Sub. I mentioned it only to indcate that the default, in Basic4PPC Subs, is parameter passing by value not by reference.

Mike.
 
Top