Skip to main content
Engineering LibreTexts

4.1: Variables

  • Page ID
    54248
  • \( \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}}\)

    The basic concept in a program is the concept of a variable. Variables in a program are like variables in an algebraic expression. They are used to hold values and then write mathematical expressions using them. Fortran allows us to have variables of different types.

    A variable can hold one value at a time. If another value is placed in the variable, the previous value is over-written and lost.

    The variable concept.

    Variables must be declared at the start of the program.

    Variable Names

    Each variable must be named. The variable name is how variables, which are memory locations, are referred to by the program. A variable name must start with a letter, followed by letters, numbers, or an underscore (“_”) and may not be longer than 32 characters. Capital letters are treated the same way as lower-case letters, (i.e., “AAA” is the same variable as “aaa”).

    For example, some valid variable names are as follows:

    x
    today
    next_month
    summation10
    

    Some invalid examples include:

    1today
    this_is_a_variable_name_with_way_way_too_many_characters_in_it
    next@month
    next month
    today!
    

    Note that the space (between next and month) or the special character, @, is not allowed. Additionally, each variable must have a type associated as explained in the following sections.

    Keywords

    In programming, a keyword is a word or identifier that has a special meaning in a programming language. For example, in the “hello world” Fortran program from the previous chapter, the word program has a special meaning in that it is used to note the start or beginning of a program. Additionally, the word write has a special meaning to note an output action (e.g., writing some information to an output device, like the screen).

    Such keywords are reserved in that they cannot be used for anything else such as variable names. That is, a variable name of program or write is not allowed.

    As additional Fortran 95/2003/2008 statements and language constructs are explained, more keywords will be identified. In general, words used for Fortran language statements, attributes, and constructs will likely be keywords. A complete list of keywords or reserved words is located in Appendix 7.


    This page titled 4.1: Variables 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.

    • Was this article helpful?