Skip to content

Types, data objects, declarations

Types

God is real… unless explicitly declared an integer

Default implicit typing depending on the first letter of the identifier. Advice: deactivate it in all your programs with the following statement:

implicit none

This statement must be before all the declarations. So a simple Fortran program should look like this:

PROGRAM name
   IMPLICIT NONE
   {declarations}
   executable constructs
END PROGRAM name

Declaration of variables

Write a line with a type followed by a list of variables. Examples:

integer i, j
real x
real u, v ! speed

Numerical operations

The relational operators on integer and real numbers are: <, <=, == for test of equality, /= means “different than”, >=, >. The result is a logical value. Example:

x = i == 3

The numeric operators are: + - * /. ** for the exponentiation.

Integer type

  • Integer literal constants: without a decimal point

  • Example of declaration of a variable:

    integer i
    
  • Largest integer value: huge(0) (depends on the compiler, but usually ). Note the difference with Python: in Python the integer values are unlimited.

  • Nota bene: / with integer operands gives an integer result (the floor of the ratio). Examples : 1 / 2 equals 0, 5 / 2 equals 2

  • Some intrinsic functions that you can apply to integers:

    • abs(a) absolute value

    • mod(a, p): a modulo p. That is: a – (a / p) * p

Real type

Real literal constants: can be without exponent: -8.4, or with exponent: 1e4, 64.2e-5.

Example of declaration of a variable:

real x

These three properties depend on the compiler:

  • Smallest non-null positive real value: tiny(0.)(usually )

  • Largest real value: huge(0.) (usually )

  • Smallest such that : epsilon(0.) (usually )

Character type

  • Write character literal constants between single or double quotes. Examples: '*' 'A' '9' "x = ?" "Isn't valid" 'Created file "plouf.txt".'

  • Length of a string: number of characters. Examples of declaration of variables:

    character(len=5) a
    character b
    
    (default length is 1)

  • Concatenation operator: //

Logical type

  • Logical literal constants: .false. .true.

  • Example of declaration of variable:

    logical found
    

  • Logical operators: .not. .or. .and. .eqv. .neqv.

Nota bene : logical and integer types are different. You cannot write an integer value in lieu of a logical one (unlike in other languages, such as C or Python).

Declaration of a named constant

Adding the parameter attribute to the declaration of an object means the object is a named constant and not a variable. You have to provide the value of the named constant in its declaration statement. Examples:

    real, parameter:: pi = 3.14159265
    integer, parameter:: m = 5, n = 10

Nota bene : the coma before parameter and the double colon :: are compulsory.

Double precision real

  • This is a second "kind" of real type.
  • Declaration :
    double precision x
    
    instead of:
    real x
    
  • Literal constants: -8.4d0, 1d4, 64.2d-5 instead of: -8.4, 1e4, 64.2e-5
  • In principle, the properties of this kind of real type depend on the compiler.
    • Smallest positive value: tiny(0d0) (usually # )
    • Largest value: huge(0d0) (usually # )
    • Smallest such as 1d0 + > 1d0: epsilon(0d0) (usually # )