Creating, Running, and Debugging an Executable Program

The example below shows a sample Fortran main program using free source form that uses a module and an external subprogram.

The function CALC_AVERAGE is contained in a separate file and depends on the module ARRAY_CALCULATOR for its interface block.

The USE statement accesses the module ARRAY_CALCULATOR. This module contains the function declaration for CALC_AVERAGE.

The 5-element array is passed to the function CALC_AVERAGE, which returns the value to the variable AVERAGE for printing.

The example is:

! File: main.f90
! This program calculates the average of five numbers  
  PROGRAM MAIN  
    USE ARRAY_CALCULATOR
    REAL, DIMENSION(5) :: A = 0
    REAL :: AVERAGE  
    PRINT *, 'Type five numbers: '
    READ  (*,'(F10.3)') A

   AVERAGE = CALC_AVERAGE(A)
    PRINT *, 'Average of the five numbers is: ', AVERAGE  
  END PROGRAM MAIN

The example below shows the module referenced by the main program. This example program shows more Fortran 95/90 features, including an interface block and an assumed-shape array:

! File: array_calc.f90.
! Module containing various calculations on arrays.  
  MODULE ARRAY_CALCULATOR
    INTERFACE
      FUNCTION CALC_AVERAGE(D)
        REAL :: CALC_AVERAGE
        REAL, INTENT(IN) :: D(:)
      END FUNCTION CALC_AVERAGE
    END INTERFACE  
  ! Other subprogram interfaces...  
  END MODULE ARRAY_CALCULATOR
         

The example below shows the function declaration CALC_AVERAGE referenced by the main program:

! File: calc_aver.f90.
! External function returning average of array.  
  FUNCTION CALC_AVERAGE(D)
    REAL :: CALC_AVERAGE
    REAL, INTENT(IN) :: D(:)
    CALC_AVERAGE = SUM(D) / UBOUND(D, DIM = 1)
  END FUNCTION CALC_AVERAGE

Commands to Create a Sample Program

During the early stages of program development, the sample program files shown above might be compiled separately and then linked together, using the following commands:

ifort -c array_calc.f90
ifort -c calc_aver.f90
ifort -c main.f90
ifort -o calc main.o array_calc.o calc_aver.o

 In this sequence of commands:

The order in which the file names are specified is significant. This ifort command:

Running the Sample Program

If your path definition includes the directory containing calc, you can run the program by simply entering its name:

calc

When running the sample program, the PRINT and READ statements in the main program result in the following dialogue between user and program:

Type five numbers:

55.5

4.5

3.9

9.0

5.6

Average of the five numbers is:   15.70000

Debugging the Sample Program

To debug a program with the debugger, compile the source files with the -g option to request additional symbol table information for source line debugging in the object and executable program files. The following ifort command also uses the -o option to name the executable program file calc_debug:

ifort -g -o calc_debug array_calc.f90 calc_aver.f90 main.f90

See also Debugging Overview and related sections.