> tutorial > variables| Documentation |
|
What is a variable ? A variable is an area into memory to stock data.
Which data ? For example, the color of one car, the best score of one player ... and so on.
Variables in programming are indispensable. We do nothing without variables. So, imagine a program that will ask how old an user is. You wish to print a message if the user is major or minor.
How to do that without variables :-) ? It's symply impossible.
A variable in Gambas owns a datatype. For example, for the age of this user, the value is an interger so, the datatype of this variable will be an interger type. We have in Gambas several datatype, like :
How to use a variable ? Before, we must declare it, i.e "create" it before to use it.
![]() | In Visual Basic, it was optional except if you enabled the explicit option. |
In Gambas, you must declare all your variables. That's a good way to well develop :-) . That allows you to avoid to make some mistakes.
Here is the way to declare one variable :
DIM variable AS Integer
You can assign a value like that :
DIM var1 AS Interger = 5 DIM myNickname AS StringmyNickname = "GarulfoUnix" 'This is another way to assign a value
Right now, let's have a look at how to print this variable by using the PRINT statement :
' Gambas module file PUBLIC SUB Main() DIM nickname AS String = "GarulfoUnix" PRINT "My nickname on internet is " & nickname END
To concatenate any variables to any strings, we use the & operator.
It's easy, isn't it ? :-)
To get some data from user, we will use the INPUT statement. Here is an example about that :
' Gambas module file PUBLIC SUB Main() DIM nickname AS String PRINT "What is your nickname ?" INPUT nickname PRINT "Your nickname is " & nickname END
When the program will arrive on this line :
INPUT nickname
It will stop to wait the data from user until user will press the Return key.