Skip to main content
Engineering LibreTexts

4.3: Declarations

  • Page ID
    54250
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Fortran variables must be declared before executable statements. This section provides an introduction to how variables are declared.

    Declaring Variables

    Declaring variables formally defines the data type of each variable and sets aside a memory location. This is performed by a type declaration statement in the form of:

    <type> :: <list of variable names>
    

    The type must be one of the predefined data types (integer, real, complex, character, logical) as outlined in the previous section. Declarations are placed in the beginning of the program (after the program statement).

    For example, to define an integer variable today,

    integer :: today
    

    Additional examples include:

    integer :: today, tomorrow, yesterday
    real :: ans2
    complex :: z
    logical :: answer
    character :: myletter
    

    The declarations can be entered in any order.

    Additional information regarding character variables is provided in chapter 11.

    Variable Ranges

    The computer has a predefined amount of space that can be used for each variable. This directly impacts the size, or range, of the number that can be represented.

    For example, an integer value can range between −2,147,483,648 and +2,147,483,647. Fortunately, this is large enough for most purposes.

    The range for real values is more complex. The range is approximately \(\pm1.7 \times 10^{\pm 38}\) supporting about 7 digits of precision.

    Type Checking

    The variable type declaration is enforced by the compiler. For example, if a variable is declared as an integer, only an integer value (a whole number) is allowed to be assigned to that variable. Attempting to assign a value of 1.75 to an integer variable could cause problems related to loss of precision. This restriction is related to the fact that the internal representations for various types are very different and not directly compatible. The compiler can sometimes recognize a type mismatch and implicitly (automatically) perform a conversion. If this is done automatically, it is not always clear and could lead to errors. As such, it is generally considered poor programming practice.

    Conversions between types should be performed explicitly. Later chapters provide specific examples of how this can be accomplished.

    When initially learning to program, this may seem quite annoying. However, this type mismatch can cause subtle errors that are difficult to find.

    Initialization

    It is possible to declare a variable and set its initial value at the same time. This initialization is not required, but can sometimes be convenient.

    For example, to define an integer variable todaysdate and set it to the 15th of the month:

    integer :: todaysdate=15
    

    Additional examples include:

    integer :: todaysday=15, tomorrow=16, yesterday=14
    real :: ave = 5.5
    

    Spaces or no spaces are allowed between the variable name. The variable declaration may or may not include an equal signs (for initialization). Commas are used to separate multiple variable declarations on the same line. Variables initialized at declaration can be changed later in the program as needed.

    Constants

    A constant is a variable that cannot be changed during program execution. For example, a program might declare a variable for \(\pi\) and set it to 3.14159. It is unlikely that a program would need to change the value for \(\pi\). The parameter qualifier will declare the variable as a constant, set the initial value, and not allow that initial value to be altered during the program execution.

    For example, the declarations:

    real, parameter :: pi = 3.14159
    integer, parameter :: width = 1280
    

    will set the variable pi to 3.14159 and width to 1280 and ensure that they cannot be changed while the program is executing.


    This page titled 4.3: Declarations is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.