The Basics of Variable Definitions

A variable in a computer program is a name for a memory location, where we can store information such as integers, real numbers, and characters. See the section called “Electronic Memory” for an explanation of computer memory. This differs from variables in algebra, which are simply abstract representations of unknown values. Be sure not to confuse the two.

A Variable definition allocates one or more memory cells for storing data, and binds the data type and a variable name to that memory address. High-level language programmers generally don't know or care what the actual memory address is. We just use the variable names to refer to all data stored in memory.

Note

In C, a definition and a declaration are not the same thing. The latter does not allocate space, but only alludes to something defined elsewhere, to provide information about data types. This will be clarified later when we discuss various types of definitions and declarations. For now, just keep the two terms separated in your head. To avoid confusion, we will adhere to these terms throughout this text when discussing any language.

Variable definitions in C:

// Variable definitions for the main program
double  radius, area;
        

Variable definitions in Fortran:

! Variable definitions for main program
double precision :: radius, area
        

Caution

The original Fortran language included a rule known as the I through N rule, which allowed programmers to omit variable definitions, and allow the compiler to define them implicitly. The rule states that if a variable is not defined explicitly, and the variable's name begins with any letter from I to N, then it is an integer. Otherwise, it is real number.

This rule may seem insane in today's world, but it was created in the days when programs were stored on paper punch cards and each line of code meant another punch card, so eliminating variable definitions saved a lot of work and paper. On today's computers, there is no reason to keep such a rule, but nevertheless, Fortran has maintained it so that old code will still compile.

If you forget to define a variable explicitly, the Fortran compiler will quietly define it for you using the I-N rule, and often not with the data type you need.

The I-N rule should always be disabled in new Fortran code by adding the line

implicit none
        

above the variable definitions in every subprogram, including the main program. This will prevent variables from being accidentally defined with the wrong type.

Variable definitions are discussed in greater detail in the section called “Variable Definitions and Declarations”.

Practice

Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
  1. What is a variable in a computer program?

  2. What does a variable definition do?

  3. How does Fortran's I-N rule work?

  4. How can we prevent the I-N rule from causing problems?