Shop OBEX P1 Docs P2 Docs Learn Events
Recursion — Parallax Forums

Recursion

SANSAN Posts: 29
edited 2009-10-02 22:48 in BASIC Stamp
Sir,
I want to write a programme with a recursive function - means a user defined function with parameters and return value and recursion.
Is it possible in PBasic language ?
Which processor I should use for that ?

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-10-01 18:13
    Nope. Recursion on the BS2 is very finite (as opposed to the seemingly infinite recursion on a PC) if you plan on using a stack based call structure. You are limited to something like 7 levels of calls before the program interpreter starts going off the rails. Of course, you can use "recursion" by calling GOTO, but that is just another way to write a loop, not really recursion.

    The BS2 doesn't support parameters or return values, recursion or no. I don't think any version of BASIC does, but I haven't looked.

    The Propeller supports recursion, but it too is limited in the number of levels although the Propeller can certainly support many more recursive calls than the BS2. I think you'll run into the same problem with pretty much any microcontroller.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-01 18:13
    PBasic doesn't support parameters and return values and it has a very small call stack. Practically speaking, a Stamp will not do what you want.

    The Spin language used on the Propeller will do what you want. It directly supports recursion, parameters, and return values as well as local variables. Have a look at the Propeller Manual and/or the Propeller Education Kit tutorials. There's 32K bytes of memory available for your compiled program, global variables, and the stack which uses all the space from the end of your program to the end of the 32K bytes of memory.
  • dev/nulldev/null Posts: 381
    edited 2009-10-01 19:30
    The Javelin chip will also do (but beware there is no garbage collector).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • RiJoRiRiJoRi Posts: 157
    edited 2009-10-02 16:47
    From what I've read, recursion is a memory-expensive way to do a loop. I'm really stretching the "little grey cells," but I think a do-while loop (or while-wend loop) can replace recursive functions.

    --Rich
    Remember, the root of "recurse" is "curse"! [noparse]:D[/noparse]
  • LeonLeon Posts: 7,620
    edited 2009-10-02 16:56
    Even with languages that use recursion, like LISP and Scheme, recursive functions are often converted to conventional code by the compiler or interpreter.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • SANSAN Posts: 29
    edited 2009-10-02 19:30
    Thank u guise.....
    In Propller chip if I load my program into EEPROM then the 32KB RAM will be free for recurssion ?
  • SRLMSRLM Posts: 5,045
    edited 2009-10-02 22:48
    You might want to poke around the Propeller documentation some more... Propeller programs can only be executed from RAM. They simply live in the EEPROM for when power goes away.

    All recursive algorithms can be converted to iterative algorithms, and vice versa. It may not be as elegant to do it the other way, but it is mathematically possible.
Sign In or Register to comment.