pjhayward's Pascal Tutorial

pjhayward.net Home

Got a question?

Tutorial Home
Lesson One - Sample Program
Lesson Two - Program Structure
Lesson Three - Data Types
and Constants
Lesson Four - Variables
Lesson Five - Text I/O
Lesson Six - Subroutines
Lesson Seven - Conditional
Lesson Eight - Arrays
Lesson Nine - Loops
Lesson Ten - Units
Lesson Four - Variables

Declaring a variable instructs the compiler to create a space in memory for you to store some data. There are countless ways to use the variables once you use them, but only a few ways to declare them in the first place.

First off, Pascal requires that you declare your variables before you use them. That's why your variable declarations are actually separate from the body of your program or subroutine code. Going back to lesson 2, you will see that the var statement, with the associated variable declarations, is separate from the begin..end. section of the program. Variables declared outside of the main program, and outside of any subroutines, are called global variables. It's generally a good idea to NOT use many global variables, because naming conflicts can occur, and it's generally more confusing when you try updating your program later.

What you name your variables is ultimately up to you. However, there are some guidelines that will help keep your code cleaner and easier to maintain.

First, name your variables based on what it will be used for. For example, if you're using an integer as a loop counter, call it LoopCounter, or Counter, or perhaps Index. Avoid the tendency to use single letter variable names, because it can become confusing what exactly the variable is doing. A common exception to this is actually the use of single letter variable names for loop counters - as long as that's ALL you use it for! If you are planning on using it to reference an array, use a more descriptive name, such as ArrayIndex.

Pascal is case-insensitive. What this means is that index, Index, iNdEx, and any other variation of INDEX will all refer to the same variable. With that said, it's usually a good idea to format your variables in such a way as to recognize them easily as variables. A common practice is to capitalize the first letter of each word in the variable name. A popular variant of this is to leave the first letter lowercase, resulting in variable names such as "someVariableName" instead of "SomeVariableName". Choose your capitalization style, and stick with it.

Variable names can contain letters, numbers, and underscore (_) characters. Variable names cannot start with a number. Trying to do so will result in the compiler complaining to you about your bad code. Variables can not contain spaces, so either concatenate all the words together (as in the "someVariableName" example above) or replace the spaces with underscores - "some_Variable_Name". Generally, just omitting the spaces is simpler.

When you declare a variable, you will specify first that you are declaring variables. That is done with the "var" reserved word. After that, you will specify the variable names you wish to declare, followed by the variable type. For example:

Some important items to note: between the variable name and the variable type, you must have a colon character. You must separate variable types with a semicolon. A new line per variable or variable type is often a good idea. You can declare multiple variables with the same variable type - see the charOne and charTwo declaration on the fourth line.

One final note about variable names. It will be tempting to use certain words for variable names that aren't allowed by the compiler. One I often have to avoid using is "end". The compiler has a list of reserved words that cannot be used as variable names, because they represent some special function of the programming language. My example, end, marks the end of a block of code. Other reserved words include break, begin, program, procedure, function, case, switch, for, while, do, repeat, if, then, else, and many more. In general, if your compiler complains about it, change the variable name.