Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: SUBROUTINES

Subroutines are similar to functions in that they perform a specific task that can be called in the main program multiple times. The difference is that subroutines do not necessarily return a value or if they do return values they are assigned names based on the input names provided in the subroutine call. They do not assign a returned value to the subroutine’s name. The basic format for a subroutine is
CONTAINS
   SUBROUTINE subroutine_name(in_var1,in_var2, ,in_var_n)
      IMPLICIT NONE
      [declaration]
      [execution]
      [subprogram]
   END SUBROUTINE subroutine_name
Like a function subroutines do not need input variables and can simply perform a specified task. The input variables can be both values that are entered into the subroutine as well as names for the output values. To define the use for input variables one must define the INTENT of the variable with INTENT(IN), INTENT(OUT) or INTENT(INOUT).
  • INTENT(IN): Used to define a variable in the subroutine as one that will be used to pass information into the subroutine for the execution. The value of the variable can not be changed in the subroutine and its value is not saved anywhere for use in the main program.
  • INTENT(OUT): Used to define a variable in the subroutine as one that will be used to pass information out of the subroutine for the program to use. The value of the variable can change in the subroutine and the most recent value is passed to the program.
  • INTENT(INOUT): Used to define a variable in the subroutine as one that will be used to pass information into the subroutine for the execution. It can also have its value changed and be passed back to the program as a new saved value.
The format for using any of the INTENT statements is
type, INTENT(IN [or] OUT [or] INOUT) :: variable_names
CALL
In order to call a subroutine for use one must use the CALL function. This is accomplished by
CALL subroutine_name(in_variables,out_variables)
!remember it can be called without input variables