Nimrod Programming/Variables
Storing Values
[edit | edit source]We learned how to print something (using echo) but our program it's right now incredibly static, it'll just say the same thing again every time we execute it, not very instersting. So how we can solve it? Using something called variables, before explaining them let's consider an example:
echo ("Hello world!")
What this will do it's to write on the console "Hello world!". But always "Hello world!" it's boring we want another phrase here, we could change every line (in computer science those "lines" are called statements ), but if we wanted to change it a lots of times it would be very time consuming. So what we can do? We can use something called variables. A variable it's basically like a case where we can store information (numbers, characters, sentences), then only thing we have to do it's to announce the existence of that variable, if we don't do that the compiler (which also checks for errors) will thing we made a mistake and won't compile (and hence we won't be able to execute this). The above example with variables would be like this:
var sentence = "Hello world!"
echo (sentence)
Neat. You want to change the phrase which is displayed? Fine, just change the variable sentence and every line will change. But what we did? Let's take a careful read about that new example.
The first line is this:
var sentence = "Hello world!"
Indeed, we are doing here two things. We first tell the computer to initialize a new variable (using the reserved keyword var
) and its name (in this case the variable it's called "sentence"), this first step is called declaration (we are declaring the existence of a variable). The second thing we did is to assign a value to our variable, now our variable represents that value, writing somewhere sentence would be as if we wrote "Hello world!".
Since the first line is understood, we should move on to the others:
echo (sentence)
As you can see be tell the computer to print what is inside the sentence variable (in the example it was "Hello world!"), the computer searches for a variable called sentence in its memory and display its contents.However, you'll be wondering one thing, why we did put double quotes on the first example but not on the second? Because of the fact the second example was a variable and the first not, when we to take one string as it is (and not consider it an expression) we must put double quotes before and after the sentence (just as you would do in English).
One last thing it's the variable have types: they can be integers, decimal numbers, strings. And once declared one variable can't change its type in Nimrod. We didn't say which type our variable is; how can the computer know which type it is? We assigned some value to our variable, Nimrod it's pretty smart and understands you want your variable to be the same type as what you put there (in this case it's a string). In the case we didn't assign anything we should make explicit it's type explicitly:
var sentence: string
string it's how the string type it's called in Nim. As a side note you should know that variables are stored in computer's RAM (indeed, that's RAM's job, to store values needed by applications) and thus it's not infinite, but you should also not worry too much since today's computers have a sheer amount of RAM and can have millions of these variables.