Now recursion is useful in certain situations, and one of those is writing compilers. However ARMbasic is written to do efficient and easy control in an embedded environment, and while it does have string functions, the intention was never to write a compiler in BASIC.
But the question of doing simple recursion comes up from time to time. Recursion is allowed in BASIC now, but you have to manage your own private stack. Could this be done automatically in a future BASIC (quite possibly, remember sub/functions were additions and floating point was recently added). But recursion is really the exception in embedded programming, not the rule, so it may take the form of a keyword that allows for recursive functions.
A classic example of a recursive routine is calculating a factorial (yes there are non-recursive solutions too)
Code: Select all
DIM privateStack(32)
privateStackPointer=0
factorialValue=0
Factorial: ' called with privateStack(privateStackPointer)
if privateStack(privateStackPointer)=1 then
FactorialValue = 1
return
endif
privateStackPointer += 1
privateStack(privateStackPointer) = privateStack(privateStackPointer-1)-1
gosub Factorial
privateStackPointer -= 1
FactorialValue *= privateStack(privateStackPointer)
return
main:
for i = 1 to 10
privateStackPointer=0
privateStack(privateStackPointer)=i
gosub Factorial
print i, FactorialValue
next i
Code: Select all
ARMbasic[9.27h] on the PC Copyright 2013, Coridium Corp.
*+
... ( 0.22K code + 0.00K const)/96K 0.15/9K data programmed
Executing...
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
... Finished in 0 ms