Skip to main content
Engineering LibreTexts

15.7: Subroutines

  • Page ID
    54363
  • \( \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}}\)

    A subroutine is a Fortran subprogram that can accept some kind of input information and based on that information, return a result or series of results.

    The general form of a subroutine is a follows:

    subroutine <name> ( <arguments> )
    <declarations>
    
        <body of subroutine>
    
        return
    end subroutine <name>
    

    The information, in the form of arguments, is passed from the calling routine to the subroutine. Each of the passed arguments must be declared and the declaration must include the type and the intent. The arguments in the calling routine and the subroutine must match and are matched up by position.

    For example, given the following simple program to find the sum and average of three numbers.

    program subExample
    
    implicit none
    real :: x1=4.0, y1=5.0, z1=6.0, sum1, ave1
    real :: x2=4.0, y2=5.0, z2=6.0, sum2, ave2
    
        call sumAve(x1, y1, z1, sum1, ave1)
        write (*,'(a,f5.1,3x,a,f5.1)') "Sum=", sum1,    &
                                "Average=", ave1
        call sumAve(x2, y2, z2, sum2, ave2)
        write (*,'(a,f5.1,3x,a,f5.1)') "Sum=", sum2,    &
                                "Average=", ave2
    contains
    
    subroutine sumAve (a, b, c, sm, av)
    real, intent(in) :: a, b, c
    real, intent(out) :: sm, av
    
        sm = a + b + c
        av = sm / 3.0
    
        return
    end subroutine sumAve
    
    end program subExample
    

    The arguments in the first call (x1, y1, z1, sum1, and ave1) are matched up to the arguments in the subroutine (a, b, c, sm, and av) by position. That is, the x1 from the call is matched with the a in the subroutine. The arguments in the second call (x2, y2, z2, sum2, and ave2) are again matched up to the arguments in the subroutine (a, b, c, sm, and av) by position. While the names of the variables do not need to match, the data types must match. Variables declared in a function or subroutine are not the same as variables in the calling routine. This is true, even if they are the same name!


    This page titled 15.7: Subroutines 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?