? The VAR command/function ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Migrating to a new website for SmileBASIC 4

The VAR command/function

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.
Author
Mariominer
Updated
Rating
3 votes
Categories
Keywords
  • VAR
  • command
  • function
  • challenge
  • variable
17 Comment(s) Javo118 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 ;) joelable joelable First Day Joined on the very first day of SmileBASIC Source Website Night Person I like the quiet night and sleep late. Express Yourself Video Games I like to play video games! Hobbies 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. Javo118 Javo118 Yeah, I do know that, I was just trying to add something to this post ;). Thanks anyway 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I didn't know you could do VAR variable=number! ahavasandwich ahavasandwich Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself Pokemon Is Awesome! I love Pokemon! Express Yourself Minecraft Is Awesome! I love Minecraft! Express Yourself 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. blizord blizord Using VAR in SmileBASIC makes for better programming habits. Most modern languages need VAR(or some matter of initialization. int, chr, etc.) SquareFingers SquareFingers 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.) ahavasandwich ahavasandwich Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself Pokemon Is Awesome! I love Pokemon! Express Yourself Minecraft Is Awesome! I love Minecraft! Express Yourself Ah, that makes sense. Thanks. :) Mariominer Mariominer First Day Joined on the very first day of SmileBASIC Source Website Promoted Page My page was promoted on SmileBASIC Source! Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength 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. randomous randomous Owner Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies 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. Mariominer Mariominer First Day Joined on the very first day of SmileBASIC Source Website Promoted Page My page was promoted on SmileBASIC Source! Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength 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. randomous randomous Owner Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies What's wrong with replying? can you post it on the bug report thread? Mariominer Mariominer First Day Joined on the very first day of SmileBASIC Source Website Promoted Page My page was promoted on SmileBASIC Source! Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength If this doesn't work I swear I will swear more than ever thought humanely possible... SquareFingers SquareFingers if we say ?LEN(VAR("RATINGS")), it will say 1 With the code you give, it actually says 21. SquareFingers SquareFingers 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" SquareFingers 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). 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself VAR variablename... hmm there really is no syntax for this type of thing AFAIK