Today is the first time I've played with the BBC's function and procedure constructs.
Just curious - were functions and procedures supposed to be defined at the end of BASIC code? I'm used to modern languages and the idea of defining something before I use it. The only way I could get this to work was to jump over the DEF block with a GOTO like this:
5 GOTO 100
10 DEF FNTWELVE
20 B=10
30 A=2
40 =(B+A)
50 REM
100 PRINT FNTWELVE
110 END
How would procedures and functions usually be defined and called? Would they typically be defined at the bottom of code or is there something I'm missing?
If this pains you, try not using GOTOs. Just have a main() procedure that you call right at the start. Define everything after the END and before PROCmain:
For a one-liner, you can put the DEF early, because DEF is interpreted as REM.
For a multi-liner, you can't execute the definition, you need to call it, so it will generally go at the end of the program - perhaps after an END.
When a function is first called, the interpreter will search for the DEF, and then store a record of where it was found (I think). So it doesn't hurt performance to have a long program with DEFs at the end. I think.
The usual construct I use is:
REM > progname
setup
setup
setup
REPEAT
main loop
main loop
main loop
UNTIL finished
END
DEFFNthing
=result
DEFFNthing
=result
DEFPROCthing
ENDPROC
DEFPROCthing
ENDPROC
etc.
For instance:
REM > DEMO
MODE 7
DIM wotnot:thing=foobar:etc.
ON ERROR IF FNerr THEN PROCexit:END
REPEAT UNTIL FNmenu
PROCexit:END
DEFFNmenu:CLS
blah blah
=quit
DEFPROCexit
blah blah
ENDPROC
etc.
I put the definitions at the end just like JGH, usually starting somewhere like line 10000 to give me plenty of headroom for the main loop. It can always be renumbered later if necessary.
SKS1 wrote: ↑Thu Mar 21, 2024 7:49 am
If this pains you, try not using GOTOs. Just have a main() procedure that you call right at the start. Define everything after the END and before PROCmain:
Thanks for the responses everyone. It is a bit of change in mindset moving away from multiple source files and the likes. The closest I ever got to using PROCs was of course GOSUBs back in the day with CBM V2 BASIC- but of course I always had those at the bottom of the program code.
Then you can create you program in as many subparts as you want by doing, eg, *EXEC MAKE. This become really useful when you have library routines such as link that you reuse in different programs instead of having to retype them:
jgharston wrote: ↑Thu Mar 21, 2024 2:08 pm
You can use multiple source files if you want. Create a text file (eg called MAKE) with contents along these lines:
There's not much you can't do with BBC BASIC is there? Thanks for that! I keep thinking variables like TOP are only internal names within the BASIC source itself.
The other thing to remember is that END can be anywhere in a BBC BASIC program, not just the last line, and there can be multiple ENDs, or none, as BBC BASIC doesn't require a program to be terminated with an END keyword (unlike Atom BASIC for instance).
Functions and procedures should be stored in the program where they are not likely to be visited by the interpreter whilst running through the program, ie. the interpreter shouldn't encounter a DEF keyword while RUNning, only PROC or FN. This is why functions and procedures are usually stored towards the latter part of a program after an END, or sometimes where the program sets up a "forever" loop, after an UNTIL0, etc.
The first line could be an END if the program is entirely made up of functions and procedures you might want to use in immediate mode. I do this occasionally; once the program is RUN, it ends immediately but has set up BASIC for knowing where the functions and procedures are so they can be called from the BASIC prompt.