Multiline functions in Commodore 64 BASIC

So, I would like to write larger functions in Commodore 64 BASIC. So far, from what I see from other sources (such as the various C64 wikis, as well as the user guide for C64 itself), function definitions can only be one line long. That is, I cannot find a similar construction in BASIC for brackets / any other languages ​​used to highlight blocks of code.

Does anyone know how I will write blocks of code in BASIC that consist of several lines?

One line function example:

10 def fn X(n) = n + 1 20 print fn X(5) rem Correctly called function. This will output 6 

But I can not do something like:

 10 def fn X(n) = 20 n = n + 1 30 print n 40 rem I'd like the definition of function X to end at line 30 above 50 fn X(5) rem Produces syntax error on line 40 

Thank you for your time!

+6
source share
3 answers

Sadly, the C64 BASIC does not support more complex features.

However, it does support more complex routines and what you want in this case.

 10 rem you can set up n in advance here 20 n = 23 30 gosub 50 40 rem n is now 24 50 rem start of subroutine; this line is not needed, it just here for clarity 60 n=n+1 70 print n 80 return 90 rem now you can call the subroutine on line 50 and it'll return at line 80 

Unfortunately, passing parameters to and returning values ​​from routines in C64 BASIC is not a formalized construct, so you just have to work with ordinary variables, as shown above.

+7
source

From what I remember, you can do this almost with colan to have multiple commands on the same line. Not the most elegant solution, but will allow you to figure out:

 10 def fn X(n) = 20 n = n + 1 30 print n 40 rem I'd like the definition of function X to end at line 30 above 50 fn X(5) rem Produces syntax error on line 40 

becomes

 10 n=n+1: print n 

Note that you cannot pass arguments, so you have to declare things and let the BASIC stack take care of this for you. Usually I would structure such programs:

 1 rem lines 1-99 are definitions. 2 n% = 0 : rem this declares the variable n as an integer, initializing it to 0 100 rem lines 100-59999 are the core code 101 n%=5 : gosub 60100 59999 end : rem explicit end of the program to ensure we don't run into our subroutine block 60000 rem lines 60000+ are my subroutines.. 60100 n% = n% + 1 : print n% : return 

It has been a while; from memory, the% symbol is what declares the variable as an integer, like $ declaring it as a string.

+1
source

You can use existing variables and mathematical commands with DEF FN , for example, if you want to PRINT from 0 to 10 inclusive in 4-bit nybbles, you could do this:

  0 DEF FN B(X)=SGN(X AND B) 1 FOR I=0 TO 10: REM OUR COUNTER 2 B=8: REM OUR BIT MARKER (128, 64, 32, 16, 8, 4, 2, 1) 3 FOR J=0 TO 3: REM WE WANT 4-BIT NYBBLES, SO 0 TO 3 INCLUSIVE 4 PRINT RIGHT$(STR$(FN B(I)),1);: REM CALLS OUR FUNCTION 5 B=B/2: REM MOVES TO NEXT BIT MARKER 6 NEXT J: REM PROCESS FOR LOOP J 7 PRINT: NEXT I: REM NEW LINE THEN PROCESS FOR LOOP I 

I tried nesting functions, but it gets too confusing. In fact, I have not seen many lists that use DEF FN . Maybe some senior hipster master BASIC programmers use them?

0
source

All Articles