I felt nostalgic recently, and decided to install a Commodore-64 emulator. C-64 BASIC 2.0 is the first language I ever programmed in... some 20 years ago (yes, C-64 was already well beyond deprecated by then).
Implementing a simple fizzbuzz in such a limited language, and un-learning pretty much everything I learned in at least the last decade to write code in a procedural paradigm with
GOTO and line numbers, was much more fun (and tricky!) than I had imagined.
Here's the code - I limited the output to 15 values, so as to be able to see the full sequence on-screen.
10 GOSUB 100 20 GOSUB 1000 99 END 100 REM CLEAR SCREEN 110 PRINT CHR$(147) 120 RETURN 200 REM MODULO 210 LET MOD% = V%-INT(V%/FB%)*FB% 220 RETURN 1000 REM INIT VARIABLES 1010 LET FIZZ$ = "FIZZ" 1011 LET BUZZ$ = "BUZZ" 1020 LET FIZZ% = 3 1021 LET BUZZ% = 5 1030 LET MIN% = 1 1031 LET MAX% = 15 1100 PRINT FIZZ$ + ":" + STR$(FIZZ%) 1101 PRINT BUZZ$ + ":" + STR(BUZZ%) 1102 PRINT FIZZ$ + BUZZ$ + ":" + STR$(FIZZ%*BUZZ%) 1105 PRINT 2000 REM ACTUAL FIZZBUZZ LOOP 2010 FOR X = MIN% TO MAX% 2015 LET RESULT% = STR$(X) 2020 LET FB% = FIZZ%*BUZZ% 2021 LET V% = X 2024 GOSUB 200 2025 IF MOD%=0 THEN LET RESULT$=FIZZ$+BUZZ$ : GOTO 2050 2030 LET FB% = FIZZ% 2031 GOSUB 200 2035 IF MOD%=0 THEN LET RESULT$=FIZZ$ : GOTO 2050 2040 LET FB% = BUZZ% 2041 GOSUB 200 2045 IF MOD%=0 THEN LET RESULT$=BUZZ$ : GOTO 2050 2050 PRINT RESULT$ 2090 NEXT X 2099 RETURN
This will sound very weird, but it's actually a pretty big concern with C-64 code, so don't laugh (ok, laugh, but it's not a joke): how's my line numbering?
I tried to write the code in such a way that it would be easy to extend into prompting the user for
MAX% values, but also for
BUZZ%; I was also concerned with implementing a "modulo" subroutine, since I knew that logic would have to be used in 3 places.
I lost quite a lot of the language's capabilities over the years (the biggest challenge was really to stop thinking in terms of objects and methods for a while). Is there something in the language that could help improve this code?
Other than that, any inconsistencies or weirdness in sight?