Jump to content


Commodore 64 Programming (Question and Answer)

C64 Commodore 64 Commodore 64 Programming BASIC Question Answer

3 replies to this topic

#1 BillyHW OFFLINE  


    River Patroller

  • 3,549 posts

Posted Sat Jan 23, 2016 1:46 PM

Hey folks,


I'm trying to self-teach how to program the Commodore 64, and a lot of folks have already recommended to me a bunch of books.  But I thought I would also start a thread to post more specific questions as they arise, and tap into the community of knowledge here.


My first question is pretty basic (har har).  I just wanted confirmation that the DEF FN command (in Commodore 64 BASIC) can only create single-variate functions of a floating point variable.  I find this very limiting and would very much like to know if there's any way to define multivariate functions, and also on arrays or strings or integers etc.  I don't think there is, and I have tried stuff but nothing works, and the books I have read so far are not 100% clear on explaining this function.


Thanks in advance for any help and advice.  I hope that other people interested in C64 programming can join in and we can build up this forum.



#2 carlsson OFFLINE  


    Metagalactic Mule

  • 8,310 posts
  • Location:Västerås, Sweden

Posted Sat Jan 23, 2016 6:03 PM

Remember that DEF FN is a way to create a formula you can reuse multiple times. It won't affect the value of the involved variables themselves.


Actually the parameter in the FN function doesn't need to be part of the formula, which this example using an array displays:


10 DEF FNA(X)=B(Y)*Y
20 B(1)=2:B(2)=3:B(3)=5
30 Y=2:PRINT FNA(0)


This also leads to the conclusion that your function can operate on multiple parameters, but only one is passed as an argument through the function call and the others should be considered as global variables:


10 DEF FNA(Y)=SC+Y*40+X
20 SC=1024:X=8


Also, the FN function works with integer variables, but probably not in the way you'd expect:


15 Y=4:Z%=4


It will print 20 twice, so in this case BASIC converts the Z% variable into floating point for the argument passing. 


The only thing FN can't do is compose strings, so if you want something like FNA(X$)=CHR$(34)+MID$(X$,Y,5)+RIGHT$(X$,1)+CHR$(34) you need a subroutine to replace it.

#3 BillyHW OFFLINE  


    River Patroller

  • Topic Starter
  • 3,549 posts

Posted Sat Jan 23, 2016 8:51 PM

Thanks for the response.


Right, so there is essentially only one local variable available to you, and it has to be a number.  That's what I was worried about.  Anything more complicated will have to be done with global variables and subroutines.


I'm also assuming that anything more complicated than a one line formula would require doing some kind of subroutine instead.  So you could *not* use a function to run some more complicated code and then return a single value.  (Correct me if I'm wrong.)


For example, this is not possible, right?


10 DEF FN A(X)

11  some code

12  some code

13  some code

14  some code

15  Return Z^2 + Z + 34


Oh, I also tried to see what would happen if you use a parameter name, like "X", that was already previously named as a global variable, and it seems to ignore the global value inside the function, but reverts back to it later.



#4 carlsson OFFLINE  


    Metagalactic Mule

  • 8,310 posts
  • Location:Västerås, Sweden

Posted Sun Jan 24, 2016 6:47 AM

No, it is a single line formula. It might be possible to use the USR(n) syntax to call a machine code routine that in its turn might be able to simulate a GOSUB call, but that is nothing I have ever considered trying and sounds quite hairy to get to work.

Also tagged with one or more of these keywords: C64, Commodore 64, Commodore, 64, Programming, BASIC, Question, Answer

0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users