
In this lesson I will briefly explain how to create a function that puts together groups of MSX Basic functions or multiple calculations that we will have to use several times in our programs. Create a single function that includes everything said above with a single command line, because perhaps in MSX BASIC we would have to do many steps and this would involve a waste of memory and perhaps a bit of confusion. These functions can be either numeric or alphanumeric.
The first thing to do is to give a name and we always do this at the beginning of the program by adding a variable to the DEF FN command, if it is alphanumeric we will add the $ symbol at the end: DEF FNA or DEF FNA$. The correct syntax is: DEF FN Function Name(Argument List)=Function definition. I'll explain step by step
its meaning. After giving the name to the function we must insert how many arguments we want to use, if there are more than one they must be divided by a comma: DEF FNA(X,Y).The variables that we insert as arguments are fictitious, they have nothing to do with the ones we will use inside the program, it just indicates that we will only have to use two of them. While in the last part of the command, we have to say what the arguments will do. For example, since we have specified 2 arguments, the first will find the square root and the second will multiply by Pigreco: DEF FNA(X,Y)= SQR(X)+Y*3.141. Below I insert the program that performs the above:
10 DEF FNA(X,Y)=SQR(X)+Y*3.141
20 I=4: J=3
30 R=FNA(I,J)
40 PRINT R
I'll give another example by creating an alphanumeric function
10 DEF FNA$(X,Y)=C$+MID$(X$,5)
20
30 FOR I=1 TO 3: READ C$
40 R$=FNA$(X,Y)
50 PRINT R$: NEXT I
60 DATA CAR, MONO, HOME
In this case, in the function definition we used the variables that we name later in the program, so the function we created will know that X and Y are equivalent
to them. What the function does; changes the first 4 characters of the word MONOVEDETTA (X$) 3 times, reading the name of the word to be changed (C$) with a READ instruction which
was saved in the DATA function. A simple function created with 2 arguments and the MID$ command.
These are just small examples but try to imagine what you can create if you are familiar with MSX BASIC; complex mathematical expressions, powerful algorithms, even with graphics it can be of great help and much more.
Thank you all. Until next time.


0 Comments