Emojicode Documentation 0.8
Guides Language Reference Package Index

Variables and Assignment

An important aspect of programming are variables. Variables pair a name with a value. The variable name can consist of any sequence of characters but may not contain spaces or emojis and may not begin with a number. These are examples of valid variable names:

newYork
incredibly-long-variable-name
send_email_to:george@washington~

There are two types of variables: mutable and constant variables. Constant variables differ from normal ones in that they cannot be changed after they were initially set.

Assigning a Constant Variable

In many cases you will only assign a variable once, i.e. store a value into it, and then use it without ever changing it. These are the cases where you should use a constant variable.

Constant variables are extremely easy to use:

31 ➑️ daysInDecember
πŸ”€EarthπŸ”€ ➑️ thirdPlanet
assignment ⟢ expression ➑️ [mutable] assignee
assignee ⟢ variable | method-call

As you can see from the examples, the value you want to assign is on the left hand side of ➑️ while the variable you want to assign the value to is on the right hand side. The above code therefore sets daysInDecember to 31 and thirdPlanet to the string Earth.

The compiler infers the type of variables from the provided values automatically.

Obviously, you cannot change a constant variable. If you try to reassign a constant variable, this will result in a compiler error.

Declaring and Assigning Mutable Variables

Sometimes, however, you need variables whose values can be modified. This is were mutable variables come in.

Before you can use a mutable variable you need to declare it. There are two ways, either you declare and assign the variable to a value in one step or you explicitly declare the variable, in which case it won’t have a value initially.

5300 ➑️ πŸ–πŸ†• money
πŸ–πŸ†• catName πŸ”‘
mutable ⟢ πŸ– [πŸ†•]
declaration ⟢ πŸ–πŸ†• variable type

The first example declares the variable money and assigns it the value 5300. The compiler will infer that the type of the variable is πŸ”’.

The second line explicitly declares the variable catName and that it is of the type πŸ”‘. It does not have a value until assigned and the compiler will raise an error if you try to use it before having assigned a value.

Changing the value of mutable variables

The point of mutable variables is its inconstancy, so let us see how you can change the value of an mutable variable.

The following is an example of assigning the two variables we declared before:

5300 ➑️ πŸ– money
πŸ”€KittyπŸ”€ ➑️ πŸ– catName

Assigning an mutable variable is very similar to assigning a constant one, but we need to note that the variable name is preceded by πŸ–. You will get an error if you omit the πŸ– and you will get an error if you try to assign a variable this way, that has not been declared.

This mechanism can prevent bugs that could emerge if you, for instance, misspell a variable.

Scoping

Variables are only accessible from the scope in which they were declared. Every code block (everything between a πŸ‡ and πŸ‰) defines a separate scope. When the code block is exited, this scope is destroyed and so are all variables and values that were declared in it. Furthermore, classes and value types define their own scope.

Operator Assignment

We’ll now have a look at another useful structure: Operator Assignment. Operator Assignment allows you to apply an operator to variable or to be more precise, an operator is applied to the value of a variable and another operand and the result of the operation is then stored into the variable.

operator-assignment ⟢ variable ⬅️ binary-operator expression

In the following example, the variable i is first incremented by one, then by 5 and finally divided by 3.

0 ➑️ πŸ–πŸ†•i
i β¬…οΈβž• 1
i β¬…οΈβž• 5
i β¬…οΈβž— 3

Operator Assignment can obviously only be used with mutable variables.

← Previous Next Up: β€œControl Flow” β†’
Something not quite right? Improve this page