What is VAR?
VAR is an essential command and a useful function. VAR is what you use to initialize variables (very important in an
OPTION STRICT program) and what you can use to dynamically manipulate variables.
How do I use VAR?
Let's start with the most important: it as a command. It is fairly simple. All you need to do is say VAR [parameters]. The parameters are just the variables you would like to declare (e.g.
VAR NAME$,AGE%,EXAGE). See, it is simple! You can have 1 item or many (e.g.
VAR NAME$,AGE%:VAR EXAGE) and they can be different types. You can also set the initial value on the same line (e.g.
VAR AGE%=9001 'IT'S OVER 9000!).
Now for the tricky part: it as a function. The syntax is
VAR(STRING REPRESENTING VARIABLE). Though a simple syntax, it's uses are interesting. If you try putting in your already defined NAME$, you would say
VAR("NAME$")="Johnson". Note the quotation marks; without them, it will try setting the variable (that's blank unless you've already set NAME$ to something) to "Johnson" and give you an error. VAR can also be used to access and modify variables in other slots. The syntax for doing that is
VAR(SLOT:VARIABLE STRING). It's all one string though, so it would be, with the NAME$ example,
VAR("1:NAME$")="Lyndon B. Johnson". The slot does not need to be a string literal; just like the variable string, it can be stored in a variable (like so):
VAR SLOT$="1"
VAR VARIABLE$="HANDS"
?FORMAT$("SLOT %S HAS %D HANDS",SLOT$,VAR(SLOT$+":"+VARIABLE$))
This generally won't do anything unless you've already ran slot 1 and it ran back to slot (whatever one this is in).
It's syntax gets off when you get to arrays. Say if you tried
DIM RATINGS[0]
PUSH RATINGS,9 'FANTASTIC!
VAR RVAR$="RATINGS"
?FORMAT$("THE RATING FOR THIS MOVIE WAS %D",VAR(RVAR$+"[0]"))
Well, I'll congratulate you on making a really interesting system, but alas, it will not work. See, instead of getting the first element in the RATINGS array, it tried to get the value of the variable
RATINGS[0] However, if we say
?LEN(VAR("RATINGS")), it will say 1, and if we do this:
DIM RATINGS[0]
DIM LIKES[0]
PUSH RATINGS,9
PUSH LIKES,6
VAR("RATINGS")=VAR("LIKES")
it will work and RATINGS[0] will equal 6.
Challenge!
Can you help the Movie Bros. and find out how to use VAR to get the first element of an array? Help, before they go bankrupt like Blockbuster, Hollywood Video, and all those before them! Bonus points if you can explain why.
More Uses
If you're like most people, you don't make systems that revolve around different groups of variables. That's ok. VAR has many other uses. Besides declaring variables you can use it for dynamic saving/loading, modifying other variables based on another program (similar to loading and saving), modifying variables in another slot, and lots of other things. Say you made a program to run other programs (I've done that before), you could make it modify variables in the program that it's running. Or if you programmed multiple sub-routine programs, you could make them modify variables in the other slots. VAR, when used properly, can make a program extremely dynamic. There are other ways to accomplish these tasks, but most of them would be more complex than VAR.
Conclusion
So as you can see, VAR is a really useful command and function. It's unfortunate that VAR's Function is undocumented. VAR's function can be used to access variables out of strings and from other slots, while it's command sets these variables. It can be used to make programs more dynamic and can be used for harmonic interactions between slots.
Thanks for Reading!
If anything I said inaccurately represents the VAR thing, let me know. If you can add to it, let me know as well. Thanks!
Thanks to SquareFingers for scrutinizing it and telling me that a lot of it was not the best representation.