Skip to main content
Engineering LibreTexts

17.1: Module Declaration

  • Page ID
    54373
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The secondary source file or module must be formatted in a specific manner as follows:

    module <name>
        <declarations>
    contains
        <subroutine and/or function definitions>
    end module <name>
    

    For example, to declare a module named stats that includes a function to find the average of the numbers in an array, the following module declaration might be used.

    module stats
    ! note, no global variables used in this module
    
    contains
    
    ! *************************************************************
    !  Simple function to find average of len values in an array.
    
    real function average(array, len)
    real, intent(in), dimension(1000) :: array
    integer, intent(in) :: len
    integer :: i
        real :: sum = 0.0
        
        do i = 1, len
            sum = sum + array(i)
        end do
        average = sum / real(len)
    end function average
    
    end module stats
    

    This example assumes the real array contains len number of values up to a maximum of 1000 values.


    This page titled 17.1: Module Declaration is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?