• Please review our updated Terms and Rules here

Local variables in BBC Micro

michaelbean510

New Member
Joined
Aug 22, 2023
Messages
1
Not sure if I'm posting in the right place, sorry if not!

I'm trying to understand local variables on the BBC Micro (not the BBC micro:bit):

DEFPROCexampleproc(J%):LOCAL I%
DEFFNexamplefunc(J%):LOCAL I%

I understand that in the above examples both J% and I% are local, and the global versions of J% and I% will be preserved.
I also understand that if the PROC calls the FN, the FN has its own local variables, and that the variables in the PROC will be preserved.

But what if the procedure calls itself... are the local variables for each iteration preserved or are they replaced each time it is called? Also, how are the original variables preserved—I'm guessing they're pushed onto the stack?

Thanks for any help!
Michael
 
Since you can use recursion on the BBC it is designed so local variables are 'fresh' for every call to the function.
Just try it out:

REPEAT INPUT "Enter an INTEGER less than 35 "num UNTIL INT(num)=num AND num<35 fact=FN_fact_num(num) PRINT num,fact END : DEF FN_fact_num(n) IF n=1 OR n=0 THEN =1 REM Return with 1 if n= 0 or 1 =n*FN_fact_num(n-1) REM Else go round again
 
Tried it on Z80 BBC basic on my 'Kaypro mini'
 

Attachments

  • BBC_recursion.jpg
    BBC_recursion.jpg
    1.3 MB · Views: 16
Last edited:
Back
Top