Rouge Documentation Help

How Rouge handles memory

Up to this point, we have only ever briefly talked about memory. Memory is a key component in any computer. Memory allows a computer to remember information that it needs at a later point. However, memory is not the only part of a computer that can save information. For instance, your computer's hard drive, also referred to as its storage, is responsible for storing information (e.g. files) for long periods of time, making it comparable to your brain's long-term memory. Your files will remain in your storage even after your computer is turned off. On the other hand, your computer's memory, also often referred to as RAM, is responsible for storing temporary data (e.g. variables) for short periods of time. This is comparable to your brain's short-term memory. Temporary data can include things like intermediate results during a calculation. Memory also loses all its contents when the computer is turned off. In programming, most of the time we are dealing with memory and only save very specific pieces of data to storage. In this course, we have only worked with memory so far and will continue to do so for the next chapters.

Creating values in memory

To begin with, let's recap what you have learned about memory in the previous chapters. You already know how to create values in memory by spelling out the value in your program. If you want to retain that value, you have to use it, either by assigning it to a variable or, for instance, by passing it to a function. We have learned that a variable is a way to access a value through a name specified by you.

// The following value is created in memory. // However, we don't have a way to access it. 42 // We can assign the value to a variable... age = 42 // ...giving us access to the underlying value. age // We can also pass it to a function to use it. is_adult(42)

When you create a value, the value is placed somewhere in the computer's memory. When you create a variable, the variable sort of points to the place in memory where the value resides. For this reason, a variable pointing to a value is also often called a reference.

In Rouge, it is not possible for multiple variables to reference the same value. When you try to create a second variable by setting it to point to the same value as the first variable, the value will be copied instead of two variables referencing the same value in memory. The following example illustrates this behavior.

a = 10 b = a b = 20 a b

Output:

10 20

First, a value of the number 10 is created in memory and assigned to the variable a. Next, the variable b is created and set to point to the same value referenced by a. However, instead of both variables pointing to the same value of 10, the value is copied in memory and b is set to reference the copy. Now, both variables reference different values in memory that do contain the same number. This becomes even clearer when we try to change the value stored in b. If both variables were to point to the same value in memory, both variables would point to the updated number 20. However, when accessing both numbers, it becomes clear that only the variable b contains the updated value.

Discarding values in memory

A computer has a limited amount of memory, which is why we need to make we don't run out of space. This is done by discarding any values that are no longer going to be used. Different programming languages have different approaches to managing the data stored in memory, with some languages even requiring you to take manual action to explicitly free up space. However, Rouge will automatically discard values that are no longer needed, making things much easier for you. Still, it is important to know about when values are going to be discarded by the language to know for how long you still have access to them.

Rouge discards values in memory by one rule only. When a value is no longer referenced from anywhere, it is going to be discarded. There are multiple scenarios in which a value becomes no longer referenced.

First, let's talk about when values are destroyed when working with functions. Any reference created inside a function body is only valid until the end of the body. When we talk about a reference being valid, it means that the reference still exists and can still be accessed.

apply_discount = function(total: Number) -> Number { first_discount = total * 0.9 second_discount = first_discount - 5 return second_discount } apply_discount(50) // The following statement will fail. // Variables created inside the function can no longer be accessed first_discount

As you can see in the above example, multiple variables are created inside the function first_discount. The variables are created at the time when the function is being called. After the function call has concluded, all references created inside the function body are destroyed, leaving the underlying values to be discarded, with one notable exception. The return value will not be destroyed because it is still needed after the function call concludes.

The space in which a variable is valid is called a scope. The language always discards variables at the end of the scope in which they were created. Many constructs in Rouge other than functions use scopes as well; however, we have yet to learn about them. A good rule of thumb is that a set of curly braces encloses a scope.

When you call a function multiple times in a row, a new scope is created and destroyed for each call. This means that the values stored in the variables inside a function do not carry over to the next function call.

At the latest, any value created during the execution of your programs will be discarded after the program has finished running. At the end of the program, all remaining references are destroyed. This happens because, at the outermost level of the program, there is actually a scope as well. When the program ends, that global scope ends as well. At that point, only unreferenced values remain in memory, all of which are then discarded by the language.

One more important detail to mention is that you can always access variables created in an outer scope, as can be seen in the following example.

voting_age = 18 allowed_to_vote(age: Number) -> Boolean { return age >= voting_age }
Last modified: 07 January 2026