Main Content

Learning to program - variables and constants

Archive - Originally posted on "The Horse's Mouth" - 2014-11-22 14:40:43 - Graham Ellis

Further material from our "learning to program in ...." courses ... an introduction to variables and constants

variable basics

Information - data - needs to be stored in a program between statements. Or rather it needs to be stored in the computer's memory. At the lowest of levels, that's a binary pattern of 0s and 1s in a numbered memory location that's encoded in such a way that it can be formed back into something that represents a number, or a string of text. I've programmed computers that work like this - in the very early days - it works, but it's pretty impractical for anything but elementary programs. So what do we do?
- We give descriptive names to the places we need to store the data
- We allow the programming language stuff to decide where to store the data in memory so we don't have to bother
- We have the programming language deal with all the low level formatting too

Variable names are typically of the programmer's choice, subject to a strict series of rules that differ from langauge to language. Typically, they'll be comprise a letter, followed by more letters, digits and underscores. Maximum name length, whether capital and lower case letters have a different meaning, whether a variable name may start with an underscore differ. Also
- In some langauges, variable names are [sometime] preceeded by a special character - a "sigil"
- And in some languages, certain words can't be used as variable names - "reserved words" such as if and break

In some languages, the programmer is required to state the names of the variable that wil be used so that the compiler can allocate memory efficiently; that also has the benefit of making the programmer thinks about exactly what's going on. In other languages, it's the langauge internals which work out what storage is needed, and how it's to be used / coded, based on the context in which it's used in the code. Although this latter solution sounds easiest to write and is good, it does have the disadvantage that it's all too easy for a variable to be misnamed and for the programmer to end up with a 'bug' that's hard to find.

On types and Scope

I've mentioned different types of data that need to be stored .. there are whole numbers, numbers with decimals, pieces of text, and others too - collections or groups of variables, booleans ("true" or "false") and indeed composite varaibles of our own type definition. As you get deeper into programming, you'll need to understand these various type.

Data sometimes need to be converted between types - for example a string of characters input by our user at the keyboard needs converting into a number on which calculations can be done. In sorme languages, this is done automatically for you, but in others you have to request explcitly that it be done.

There's also the matter, as programs grow, of how long the data in them (and the name) is retained - it would be wasteful in a long running program to retain data that's only required for a very short period as the program starts up right through to when the program finished running, but it would be frustrating if the programmer came to use a piece of data to find that it had gone away. There's the further matter here of a program that's got sections written by different programmers, and the need for variable names used by each of the programmers to be distinct from each other. Think of two families living in the same town, both who have daughters who they name "Lorna". That's find and good around the home, but when the two young ladies end up in the same class as school, the teacher says "please stand up Lorna" and both will stand up ... so there needs to be something extra. This subject is called the scope of a variable, and it's so important that we raise it to make you aware of an upcoming issue even on your very first day of programming, though solutions and detailed discussions must wait until we're further into the course.

Constants

There are some numbers - "constants" - which won't change (or you don't expect to change) in your program. There are 24 hours in a day, and 7 days in a week. And there are some values which are constant to you - the maximumm number of delegates on a public course might always be 7, the number of working days in the week might be 5, the BMI levels below which and above which a person is regarded as being unhealth may 20.0 and 25.0.

There's nothing to stop you writing these numbers directly into your program, but that's not a good idea:
* You'll find it hard to find all occurrences of the number should it ever change
* You'll write code that's confusing in the extreme if the same constant happens to apply to two things
* The code won't be very descriptive when you come to read it back.

So you'll find that early on we recommend that you assign constants into named locations, and on this first day of the course we'll use variables. However, many languages also support a special notation for named constants, and if you use that:
* Your code can run more efficiently as there needs to be no mechanism to amend a value
* In languges which statically assign memory, a whole heap of complexity can be solved is the constant is a "maximm number of ..."
* The maintenance programmer is clearly told "this value won't be changing at run time"
* The constant can be much more widely scoped so that its's available right through your code without scope conflicts.