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
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 -c option prevents linking and retains the .o files.
The first command creates the files array_calculator.mod and array_calc.o (the name in the MODULE statement determines the name of module file array_calculator.mod). Module files are written into the current working directory.
The second command creates the file calc_aver.o.
The third command creates the file main.o and uses the module file array_calculator.mod.
The last command links all object files into the executable program named calc. To link files, use the ifort command instead of the ld command.
The order in which the file names are specified is significant. Consider the following ifort command:
ifort -o calc array_calc.f90 calc_aver.f90 main.f90
This command does the following:
Compiles the file array_calc.f90, which contains the module definition, and creates its object file and the file array_calculator.mod.
Compiles the file calc_aver.f90, which contains the external function CALC_AVERAGE.
Compiles the file main.f90 (main program). The USE statement references the module file array_calculator.mod.
Uses ld to link the main program and all object files into an executable program file named calc.
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
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.