functions calling self

Blitz3D Forums/Blitz3D Beginners Area/functions calling self

Just to clarify -- if you call a function from a loop, it will NOT continue from its looping position. The new call is a whole new thing. However, once you RETURN from the new call, it will continue from the original point of recursion. This is why it's imperative that when you write a recursive function, there always be a base case to check and return from, in addition to another case which will call itself. (Note that what the base case checks should change through the recursive calls, too.)

Here's an example. Watch it perform through the debugger -- you'll see what it's doing.

Global count% = 1
Global decimal% = 10

Bits(10)
Print "It takes "+count+" bits in binary to represent decimal 10"

Function Bits%(x%)
	If x = 1 
		Return count
	Else
		count = count + 1
		x = Bits(x/2)
	EndIf	
End Function