Computer Science Canada

Variables vs Constants

Author:  scholarlytutor [ Mon Dec 07, 2020 10:31 pm ]
Post subject:  Variables vs Constants

I thought I'd write a simple tutorial today for the difference between variables and constants. This is something that I never learned properly in high school; in my over 2000 word final project for Turing, I never used const once. But there are some good reasons why sometimes we should use constants, and other times variables.

Variables are a simple way to save a number or word into memory. Maybe you're making a math program, and you need to save the correct answer to a question. To do that, you do the following:

var correct_answer : int

This creates a variable called correct answer. It's an int (integer) type, which means it can only store whole numbers. I've placed an underscore since variable names cannot have a space.

Most likely your math program will have a lot of questions, so your correct answer will keep changing. That's what variables do: They can *vary*, or change. Every time it changes, you'll put a new value inside of it. At one point, your answer might be 7 * 3, and so

correct_answer := 7 * 3

and at another point, your correct answer may be for 4 * 9

correct_answer := 4 * 9

For anyone who is unfamilar, the symbol colon equals := is called the assignment operator. It stores whatever you write on the right side into the variable called correct_answer. If you later want to show your correct answer on the screen, just use a put statement.

put correct_answer

There are many other things that can be said about variables, but those are the basics. The key thing to appreciate here is that we first make variables, we can then change its value whenever we like, and of course we can use it in our program, such as showing it on the screen using a put command.

However, say you have a number or word that you want to save in memory, but which never changes. For example, perhaps the main character of your game is called Odin, and it never changes. In programming, if you don't want something to change, you should write your code in such a way so that it's impossible to change. So instead of making a variable, you make a constant instead. For example:

const main_character : string := "Odin"

This creates a constant called main character. It's a string this time because we're saving a word, not a number (also, note that words have "speech marks" around them). Now, constants have values that never change, so you must tell Turing *immediately* what the value of your constant is. So, using the assignment operator := we give it the value of "Odin" at the end of the line.

Why is creating a constant a good thing? If you or someone else upgrading your code tried to later change the value of main character, Turing would give an error when you tried to run it. This error is actually considered a good thing: good coders want to catch errors immediately before a program runs, not by surprise while the program is running. The error reminds us that main character is not supposed to change in this program.

So to be a good coder, keep this rule in mind: If you want to save a word or number in memory and you want the program to be able to change that value, then make it into a variable. However, if you want to save a word or number in memory that you know will NEVER change in your program, you should always make it into a constant. This is one of many strategies to prevent you, as well as other people working on your code, from making some unintended errors when you add new code to your program.

And that's basically all you need to know. For those of you who already have some practice with variables and constants, I'll just add a couple of other additional tips.

Additional Tips:

Most people who write constants won't include the middle portion - this is because Turing knows how to figure out what type of data you have. So, say we are saving the amount of HST that Ontario (in Canada) charges for purchases, which is a decimal (in Turing, these are called real numbers). You can write:

const Ontario_tax := 0.13

Turing will know that's a real (decimal) variable. So this is a slightly quicker way of creating your constant.

The same format can be used for variables, too. So if you know the starting value of your variable, you can use this shorter form as well. For example, if we were creating an overall player score for a game, it will probably start at zero. So when making the variable, you can type:

var player_score := 0

Again, this is faster. However, if you are making a variable and you don't know the starting value, then you must tell Turing if it's an int, string or real number.

Finally, for those of you who know about arrays (data types that store several numbers or words instead of just one), you can make them into constants as well. Imagine we had a list of five fruits that we never wanted to change. So, we'd write:

const fruits : array 1 .. 5 of string := init ("apple", "orange", "pear", "plum", "apricot")

Understanding how arrays work requires an entire tutorial of its own. But if you already know how arrays work, and you know the values won't change while your program is running, then you should be using const instead of var.

And that's all for this tutorial. Thanks for reading.