9

My BASIC is almost working, the almost being the DEF FN. Just so I don't paint myself into a corner, is there any "mainstream" BASIC from the 8-bit era that allowed user-defined string functions?

I'm mainly using "The Basic Language" from BASIC Computer Games as my guide to the language, and it doesn't really say much on the topic. All use of DEF in the book is always math. Commodore BASICs definitely don't support string functions, the right-hand-side must be a mathematical expression, and I suspect this is true for most others derived from it.

Are there any major 8-bit dialects that supported this - TRS-80, Coco, TI-99, Sinclair, BBC, etc? Not trying to do GW or even MSX, but I would be curious to know if they did.

Maury Markowitz
  • 19,803
  • 1
  • 47
  • 138

4 Answers4

14

BBC BASIC does. Example from the manual:

100 DEF FNMID(A$)
110 LOCAL L
120 L=LEN(A$)
140 =MID$(A$,L/2,1)
phuclv
  • 3,592
  • 1
  • 19
  • 30
Tim Locke
  • 4,811
  • 21
  • 36
  • 3
    Sinclair basic does too. – Jasen Oct 18 '20 at 04:14
  • I thought Sinclair's had EVAL? – Mark Williams Oct 18 '20 at 08:35
  • 2
    Sinclair's "EVAL" was VAL() or VAL$(). – Martin Maly Oct 18 '20 at 09:47
  • Interesting, OK then I'll make sure it can do that. What does line 140 do with the result? Is this a multi-line DEF and the = is return? – Maury Markowitz Oct 18 '20 at 14:34
  • 1
    @TimLocke - Locomotive BASIC does. It's not MS derived, more MS inspired – scruss Oct 18 '20 at 15:23
  • @MauryMarkowitz, Yes, it is a multi-line DEF. DEF appears to look for a line beginning with = for the final return value. An example of a single-line DEF is:

    1010 DEF FNST(g)=1.15*g

    – Tim Locke Oct 18 '20 at 16:11
  • @TimLocke - any examples of single-line string function definitions? – Maury Markowitz Oct 18 '20 at 16:53
  • @MauryMarkowitz DEF FNb$(a$)=LEFT$(a$,1) works in MSX BASIC 1.0 and in BASIC-80 Rev. 5.21. It does not work in C128 BASIC or Tandy Model 100 BASIC, so it's one of those weird optional features like BCD maths that MS were entirely random about – scruss Oct 18 '20 at 18:35
  • 1
    @MauryMarkowitz, it's not so random. Bill Gates wasn't a fan of the MOS 6502 or the 8-bit Motorola CPUs. He only made them good enough that they would sell and he would make money. He liked the Intel CPUs and the Zilog Z80 which was based on the Intel 8008, so they got the extra features. Perhaps he saw them as business processors. The C128 has the 6502. The Tandy Model 100 ran on the Intel 8085, BASIC-80 ran on the Intel 8080 and Zilog Z80, and MSX BASIC ran on the Zilog Z80. Also, Microsoft's first product was designed for the Intel 8080 and the 6502 and Motorola versions were afterthoughts. – Tim Locke Oct 18 '20 at 20:16
  • @scruss Are you sure about the MSX-BASIC? My Philips VG8020 gives a syntax error if I try that. MSX BASIC 1.06 according to the boot message. – Tonny Oct 19 '20 at 11:31
  • Let's not forget Sinclair QL SuperBASIC. – Michael Harvey Oct 19 '20 at 12:55
  • @Tonny I'm only emulating, but it appears to work on a VG8020 with MSX BASIC 1.0. Emulated MSX BASIC 2.0 works, too. – scruss Oct 19 '20 at 14:09
  • @scruss I haven't tried your actual example (my MSX is in storage and I can't get it out at the moment) but I tried some BASIC listings from an old MSX magazine a couple of years ago and ran into the problem that the DEF FN() stuff wouldn't work. I just gave it a try in OpenMSX and it works there with the emulated VG8020/00, so there must have been an issue with those listings I presume. – Tonny Oct 19 '20 at 15:20
8

Locomotive BASIC does support string functions (as do MSX BASIC 1.0 and BASIC-80 Rev. 5.2):

10 DEF FNa(x)=x/2
20 DEF FNb$(a$)=LEFT$(a$,1)
30 PRINT FNa(10)
40 PRINT FNb$("phweeen")
Ready
run
 5
p
Ready
scruss
  • 21,585
  • 1
  • 45
  • 113
  • Nice! I looked it up in the manual but didn't see any mention of strings in relation to user defined functions. – Tim Locke Oct 18 '20 at 16:01
3

TI-99/4A definitely did. It had DEF for numeric and string functions, in normal TI-Basic and in TI-Extended Basic. Excerpt from the User manual.

DEFine
{ numeric-function-name |(parameter)| = numeric-expression } 

DEF { string-functlon-name|(parameter)| = string$-expresslon }

The DEFine statement allows you to define your own functions to use within a program. The function-name you specify may be any valid variable name. If you specify a parameter following the function-name. the parameter must be enclosed in parentheses and may be any valid variable name. Note that if the expression you specify evaluates to a string result. the (unction-name you use must be string variable name (i.e .. the last character must be a dollar sign. $). [..] User·s Reference Guide Examples: >NEW >100 REM TAKE A NAME AND PRINT IT BACKWARDS >110 DEF BACK$(Xl=SEG$(NAME$ , x, 1 ) >120 INPUT "NAME? ":NAME$ >130 FOR I=LEN(NAME$) TO 1 STEP -1 >140 BNAME$=BNAME$&BACK$(I) >150 NEXT I >160 PRINT NAME$: BNAMES >170 END >RUN NAME? ROBOT ROBOT TOBOR

** DONE **

Patrick Schlüter
  • 4,120
  • 1
  • 15
  • 22
  • 1
    The TI-99/4A wasn't 8-bit, though – scruss Oct 19 '20 at 13:56
  • Technically true but without any advantage compared to 8 bit machines.It had only 64K of address range. Except for 256 bytes SRAM and 8 Kb ROMS, everything was connected on a (slow) 8 bit bus. An MSX-1 trounces the TI. – Patrick Schlüter Oct 19 '20 at 14:14
1

Dartmouth BASIC, 5th Edition (1970) allowed user-defined string functions with string arguments. It ran on a mainframe though, not an 8-bit microcomputer.

AndyB
  • 331
  • 1
  • 5