Went back to good old qbasic for nostalgic reasons and have never used types and functions in qbasic before as I was very young that time.
TYPE Vector2
x AS SINGLE
y AS SINGLE
END TYPE
FUNCTION Vector2Mag (a AS Vector2)
Vector2Mag = SQR((a.x * a.x) + (a.y * a.y))
END FUNCTION
FUNCTION Vector2Add (a AS Vector2, b AS Vector2)
DIM r AS Vector2
r.x = a.x + b.x
r.y = a.y + b.y
Vector2Add = r
END FUNCTION
But i get
Illegal SUB/FUNCTION parameter on current line
using qb64 in both first function lines. Google didn't help as it looks like I am doing everything right. I checked passing multiple variables, specifying a type for a parameter, how to use types but nothing really helped.
Thanks guys.
Defining or using user-defined variables inside functions is illegal in QB. It doesn't matter if the function is declared by DEF FNname ... END DEF or FUNCTION ... END FUNCTION
What you can do is send a pointer to the address of a user-defined variable, then the function/sub uses the address to read it directly from memory. The elements of a userdefined variable are stored exactly in the order they are defined. In this example, a (two byte integer) will be stored first, in big endian format, then b, a total of four bytes.
TYPE xtyp
DIM a AS INTEGER
DIM b AS INTEGER
END TYPE
DIM var AS xtyp
var.a = 5
var.b = 7
DEF SEG = VARSEG(var)
PRINT "The value of var.a and var.b multiplied is"; mpl(VARPTR(var))
END
'-------------------------- End of main program, function begins here -------
FUNCTION mpl(addr)
factor1 = PEEK(addr) + PEEK(addr + 1) * 256
factor2 = PEEK(addr + 2) + PEEK(addr + 3) * 256
mpl = factor1 * factor2
END FUNCTION
DEF SEGis used to set the current segment, and VARSEG()returns the segment of a numeric or user defined variable.
PEEK()is used to read a byte from a certain memory-position, and VARPTR()returns the address of a numeric or user-defined variable in it's segment.
Note that the above code assumes both factors are unsigned. If they are signed, the transforming of individual bytes into integer numbers must be slightly different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With