BBC BASIC and Functions

bbc/electron apps, languages, utils, educational progs, demos + more
Post Reply
SparkyNZ
Posts: 68
Joined: Tue Feb 06, 2024 5:05 am
Contact:

BBC BASIC and Functions

Post by SparkyNZ »

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:

Code: Select all

    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?
User avatar
SKS1
Posts: 324
Joined: Sat Sep 19, 2020 12:04 am
Location: Highland Perthshire
Contact:

Re: BBC BASIC and Functions

Post by SKS1 »

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:

Code: Select all

PROCmain: END
:
DEF FNTWELVE
...
=result
:
DEF PROCmain
PRINT FNTWELVE
ENDPROC
Miserable old curmudgeon who still likes a bit of an ARM wrestle now and then. Pi 4, 3, ARMX6, SA Risc PC, A540, A440
User avatar
BigEd
Posts: 6261
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: BBC BASIC and Functions

Post by BigEd »

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.

Edit: crossed in the post!
User avatar
jgharston
Posts: 5319
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: BBC BASIC and Functions

Post by jgharston »

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.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.45
(C) Copyright J.G.Harston 1989,2005-2024
>_
tnash
Posts: 161
Joined: Mon May 02, 2022 9:56 am
Contact:

Re: BBC BASIC and Functions

Post by tnash »

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.
SparkyNZ
Posts: 68
Joined: Tue Feb 06, 2024 5:05 am
Contact:

Re: BBC BASIC and Functions

Post by SparkyNZ »

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:

Code: Select all

PROCmain: END
:
DEF FNTWELVE
...
=result
:
DEF PROCmain
PRINT FNTWELVE
ENDPROC
Of course. That's better still :-)
SparkyNZ
Posts: 68
Joined: Tue Feb 06, 2024 5:05 am
Contact:

Re: BBC BASIC and Functions

Post by SparkyNZ »

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.

Thanks for the sanity check!
User avatar
jgharston
Posts: 5319
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: BBC BASIC and Functions

Post by jgharston »

You can use multiple source files if you want. Create a text file (eg called MAKE) with contents along these lines:

Code: Select all

*BASIC
LOAD "part1"
OSCLI "LOAD part2 "+STR$~(TOP-2):END
OSCLI "LOAD part3 "+STR$~(TOP-2):END
OSCLI "LOAD part4 "+STR$~(TOP-2):END
RENUMBER
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:

Code: Select all

*BASIC
LOAD "part1"
OSCLI "LOAD part2 "+STR$~(TOP-2):END
OSCLI "LOAD BLIB.Number "+STR$~(TOP-2):END
OSCLI "LOAD BLIB.StringIO "+STR$~(TOP-2):END
OSCLI "LOAD BLIB.FileIO "+STR$~(TOP-2):END
RENUMBER
eg see link
Last edited by jgharston on Thu Mar 21, 2024 9:08 pm, edited 1 time in total.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.45
(C) Copyright J.G.Harston 1989,2005-2024
>_
SparkyNZ
Posts: 68
Joined: Tue Feb 06, 2024 5:05 am
Contact:

Re: BBC BASIC and Functions

Post by SparkyNZ »

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:
:shock:

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.
vexorg
Posts: 287
Joined: Wed May 24, 2023 5:05 pm
Contact:

Re: BBC BASIC and Functions

Post by vexorg »

Are these functions that return a value, or more of a function like a C program has?
From pascal days you had procedures and functions.

I think I used to use gosub more for what would be functions nowadays.
Last edited by vexorg on Fri Mar 22, 2024 4:34 pm, edited 1 time in total.
David
User avatar
geraldholdsworth
Posts: 1401
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Re: BBC BASIC and Functions

Post by geraldholdsworth »

vexorg wrote: Fri Mar 22, 2024 12:31 pm From pascal days you had procedures and functions.
Basic is almost like Pascal. It also has both functions (returning a value) and procedures (no value returned).
Gerald Holdsworth, CTS-D
Extron Authorised Programmer
https://www.geraldholdsworth.co.uk
https://www.reptonresourcepage.co.uk
Twitter @radiogezza
User avatar
BeebMaster
Posts: 7379
Joined: Sun Aug 02, 2009 5:59 pm
Location: Lost in the BeebVault!
Contact:

Re: BBC BASIC and Functions

Post by BeebMaster »

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.
Image
Post Reply

Return to “8-bit acorn software: other”