The term subprogram applies to both Fortran functions that explicitly return values and Fortran subroutines that do not.
There are four types of subprograms: intrinsic, internal, module and external. Intrinsic subprograms are built into the language and can be used by any Fortran program. Internal subprograms are part of the programs in which they are defined and can only be used within this program. Internal subprograms are defined and used in the following way:
Program ProgramName
Non-executable statements (don't declare function names).
Executable statements such as...
Call SubroutineName(...)
x = FunctionName(...)
stop
Contains
Subprogram SubprogramName1(...)
Non-executable statements.
Executable statements.
return
End Subprogram SubprogramName1
End program ProgramName
Internal subprograms can access variables in their host programs, but this is not recommended.
Module subprograms are defined and used as follows:
Module ModuleName
Contains
subprogram Subprogram1(...)
Non-executable statements.
Executable statements.
return
End subprogram Subprogram1
End Module ModuleName
program ProgramName
use ModuleName
Other non-executable statements.
Executable statments including...
Call subroutine SubroutineName(...)
x = FunctionName(...)
stop
end program ProgramName
External subprograms are defined and used as follows:
Program ProgramName
Interface
Subprogram SubroutineName(...)
Real funciton FunctionName(...)
End interface
Other non-executable statements.
Executable statements such as...
Call SubroutineName(...)
x = FunctionName(...)
stop
End program ProgramName
Subprogram SubprogramName1(...)
Non-executable statements.
Executable statements.
return
End Subprogram SubprogramName1
Modules subprograms and external subprograms do not have to be in the same source code file as the main program, but the compiler and linker must be able to find them. Rules for specifying where the compiler and linker looks for modules and external subprograms vary.