Values and variables
In this chapter, you will create your first program with Rouge. We will begin by working with numbers and variables. It might sound sobering at first, but programming mostly consists of moving and storing data in a sensible way. In this chapter, we will take a look at how to create data on the computer and store it in a way that allows you to access it at a later time. Take a look at this first program. Even though it is very short, it is still a valid Rouge program.
As previously mentioned, a program consists of multiple statements, each in its own line. A statement represents a single instruction to the computer. We can deduce that the program above only consists of one statement because it only has one line. Now, let's look at what the statement achieves. The statement creates a value in the computer's memory. In the case of the program above, the value is a number. A value is just a piece of data that you want to work with in your program. Simply by spelling out a number in the program, we create the number in your computer's memory. In the case of the program above, the piece of data is the number 42. However, the value is not useful in this form because it is not saved anywhere. Right now, the value just vanishes after it is being created. For this reason, we can save the value to a variable as follows.
A variable allows you to assign a name to a value. Now, the value is not discarded immediately after its creation, but saved in the variable. To assign something to a variable, spell out the name you would like to use for the variable followed by an equal sign followed by the value, as can be seen in the program above. The additional benefit of using variables is that values receive meaning. For instance, on its own the number 42 has little meaning, however, assigned to a variable called age it suddenly represents something. A statement that assigns a value to a variable is called an assignment. The value saved in a variable can also be updated, as can be seen in the following program.
You might have noticed that the above example contains multiple statements. The programming language simply executes the statements step by step, starting from the top. Initially, age is set to 42 which is then updated to 38. This process is referred to as reassignment. You can create as many different variables as you would like, such as in the example below.
You can choose any variable names you want, except for a few reserved names that have other uses in the Rouge programming language. You will learn about these so-called keywords throughout this course. The only other requirement for variable names is that they don't contain any whitespace.
Now that you know how to assign a value to a variable, you can use variables in place of values. In this case, the current value saved in the variable is extracted and the assignment continues in the same way previously explained.
Now that you know how to create values and save them in variables, you might be wondering about the usefulness of it all. Therefore, we would like to give you an overview of what is to come next. You will learn about how to perform complex calculations with numbers, output values to the screen, create reusable blocks of code, and different kinds of data besides numbers.