HP Fortran Programmer's Reference (September 2007)

Program units and procedures
Modules
Chapter 7194
The purpose of precision is to communicate a kind type parameter to the other program
units in the program, for the sake of precision portability. The second
module—linear_equation_solver—contains three module procedures, the first of which,
solve_linear_equations, uses the other two; solve_linear_equations is itself invoked by
the main program.
Stated algebraically, the equations that main.f90 provides as input for solution are:
2x + 3y + 4z = 20
3x + 4y + 5z = 26
4x + 5y - 6z = -4
Example 7-11 main.f90
PROGRAM main
! use the two modules defined in precision.f90 and
! lin_eq_slv.f90
USE precision
USE linear_equation_solver
IMPLICIT NONE
! the matrix a contains the coefficients to solve; b holds
! the constants on the right-hand side of the equation;
! the solution goes in x
REAL (adequate) :: a(3,3), b(3), x(3)
INTEGER :: i, j
! set by solve_linear_equations to indicate whether or not
! a solution was possible
LOGICAL :: error
! initialize the matrix
DO i = 1,3
DO j = 1,3
a(i,j) = i+j
END DO
END DO
a(3,3) = -a(3,3)
! initialize the vector of constants
b = (/ 20, 26, -4 /)
CALL solve_linear_equations (a, x, b, error)
IF (error) THEN
PRINT *, 'Cannot solve.'
ELSE
PRINT *, 'The solution:', x
END IF
END PROGRAM main