Jump to content
IGNORED

Commodore 64 Programming (Question and Answer)


BillyHW

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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
30 PRINT PEEK(FNA(7))

 

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

 

 

10 DEFFNA(X)=X+X*X
15 Y=4:Z%=4
20 PRINTFNA(Y),FNA(Z%)

 

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.

  • Like 2
Link to comment
Share on other sites

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.

 

qry89c.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...