LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

The VAR command/function

Root / Submissions / [.]

MariominerCreated:

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.

The syntax is VAR("VARIABLE NAME") This implies that the parameter to VAR must always be a string literal, which is false. More correct to say the syntax is VAR(string value).

You can also initialize the variable in the VAR statement. There is no need for two statements, one to declare, one to assign a value, you can say
VAR X=7,Y$="Hello"

if we say ?LEN(VAR("RATINGS")), it will say 1 With the code you give, it actually says 21.

UGGH REPLYING IS BEING WIERD SORRY For the LEN one, that's true. I thought it was [0], but I did [20] like a fool and then did pushing. For the initialize one, I'll put that there. Could be important. For the Syntax, I can see how that can be taken in that manner. I'll update that. I think I addressed everything.

Replying to:Mariominer
UGGH REPLYING IS BEING WIERD SORRY For the LEN one, that's true. I thought it was [0], but I did [20] like a fool and then did pushing. For the initialize one, I'll put that there. Could be important. For the Syntax, I can see how that can be taken in that manner. I'll update that. I think I addressed everything.
What's wrong with replying? can you post it on the bug report thread?

If this is outside of a box, it's being weird. If not, apparently not. I guess I'm just bad at hitting the right button.

Replying to:Mariominer
If this is outside of a box, it's being weird. If not, apparently not. I guess I'm just bad at hitting the right button.
You uhhh have to hit the "reply" button FIRST, then the reply box shows up and you can start typing in it. If you're on a touchscreen device without a mouse, you have to tap the comment first to bring up the reply button, then press the button to bring up the reply box.

Replying to:Mariominer
UGGH REPLYING IS BEING WIERD SORRY For the LEN one, that's true. I thought it was [0], but I did [20] like a fool and then did pushing. For the initialize one, I'll put that there. Could be important. For the Syntax, I can see how that can be taken in that manner. I'll update that. I think I addressed everything.
If this doesn't work I swear I will swear more than ever thought humanely possible...

So why do we use VAR instead of just saying something like ABC$="word"? I've never studied programming at school, so that's why I'm having a hard time with this stuff.

Replying to:ahavasandwich
So why do we use VAR instead of just saying something like ABC$="word"? I've never studied programming at school, so that's why I'm having a hard time with this stuff.
Using VAR in SmileBASIC makes for better programming habits. Most modern languages need VAR(or some matter of initialization. int, chr, etc.)

Replying to:ahavasandwich
So why do we use VAR instead of just saying something like ABC$="word"? I've never studied programming at school, so that's why I'm having a hard time with this stuff.
Imagine you see the following program:
HELLO$="H"
HELLO$=HELLO$+"e"
HELLO$=HELLO$+"l"
HELOO$=HELLO$+"l"
HELLO$=HELLO$+"o"
Seeing that, you might suspect there is an error on the 4th line. Without OPTION STRICT, the system will not see an error. With OPTION STRICT, and with HELLO$ declared with a VAR statement (and HELOO$ not declared), the system does see an error. (If you declare two variables with names as similar as that, it's on you if there is confusion between the two.)

Replying to:ahavasandwich
So why do we use VAR instead of just saying something like ABC$="word"? I've never studied programming at school, so that's why I'm having a hard time with this stuff.
Ah, that makes sense. Thanks. :)

I didn't know you could do VAR variable=number!

Replying to:SquareFingers
The syntax is VAR("VARIABLE NAME") This implies that the parameter to VAR must always be a string literal, which is false. More correct to say the syntax is VAR(string value).
VAR variablename... hmm there really is no syntax for this type of thing AFAIK

Awasome, this is so helpfull, btw, i did give it another use: Without OPTION STRICT, i can use the same variable without losing its original value, I mean: I=5 PRINT I CHK PRINT I END DEF CHK VAR I=8 PRINT I END This will print: 5 8 5 As you can see, the original value stay even after the function "modifies" the value of I, but the value of I inside the function is printed as well. You can use it this way too. DEF CHK VAR I I=8 PRINT I END Hope it helps ;)

Replying to:Javo118
Awasome, this is so helpfull, btw, i did give it another use: Without OPTION STRICT, i can use the same variable without losing its original value, I mean: I=5 PRINT I CHK PRINT I END DEF CHK VAR I=8 PRINT I END This will print: 5 8 5 As you can see, the original value stay even after the function "modifies" the value of I, but the value of I inside the function is printed as well. You can use it this way too. DEF CHK VAR I I=8 PRINT I END Hope it helps ;)
The value of I only doesn't change outside of the definition because, when you used VAR I=8 in the DEF, that created what's known as a local variable. The VAR I=5 outside of the DEF are global variables, so they are treated differently. This is quite confusing for someone just learning what globals and locals are, so if you want, you can look up the difference of them and how they work.

Replying to:Javo118
Awasome, this is so helpfull, btw, i did give it another use: Without OPTION STRICT, i can use the same variable without losing its original value, I mean: I=5 PRINT I CHK PRINT I END DEF CHK VAR I=8 PRINT I END This will print: 5 8 5 As you can see, the original value stay even after the function "modifies" the value of I, but the value of I inside the function is printed as well. You can use it this way too. DEF CHK VAR I I=8 PRINT I END Hope it helps ;)
Yeah, I do know that, I was just trying to add something to this post ;). Thanks anyway