Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 8.7: Generic Elemental Procedures

Adapted from: “Guide to Fortran 2008 Programming” by Walter S. Brainerd (Springer 2015)

Program to demonstrate the generic elemental procedures.

module swap_module

    implicit none

    public          :: swap
    private         :: swap_reals, swap_integers

    interface swap
        procedure swap_reals, swap_integers
    end interface

contains

    elemental subroutine swap_reals (a, b)
        real, intent (in out)   :: a, b
        real                    :: temp
        temp = a
        a = b
        b = temp
    end subroutine swap_reals

    elemental subroutine swap_integers (a, b)
        integer, intent (in out)    :: a, b
        integer                     :: temp
        temp = a
        a = b
        b = temp
    end subroutine swap_integers

end module swap_module

program generic_elemental_procedures

    use swap_module
    implicit none
    integer, dimension (3)          :: &
        i = [1, 2, 3], &
        j = [7, 8, 9]

    real, dimension (3)             :: &
        a = [1.1, 2.2, 3.3], &
        b = [7.7, 8.8, 9.9]

    print *

    write (*, '(a, 3i5)') "[*] The array i is: ", i
    write (*, '(a, 3i5)') "[*] The array j is: ", j
    
    print *

    write (*, '(a)') "[*] Now calling swap on the integer arrays...."
    print *

    call swap(i, j)

    write (*, '(a, 3i5)') "[*] After swapping, the array i is: ", i
    write (*, '(a, 3i5)') "[*] After swapping, the array j is: ", j

    print *

    write (*, '(a, 3f5.1)') "[*] The array a is: ", a
    write (*, '(a, 3f5.1)') "[*] The array b is: ", b

    print *

    write (*, '(a)') "[*] Now calling swap on the real arrays...."
    print *

    call swap(a, b)

    write (*, '(a, 3f5.1)') "[*] After swapping, the array a is: ", a
    write (*, '(a, 3f5.1)') "[*] After swapping, the array b is: ", b

end program generic_elemental_procedures

The above program is compiled and run using Fortran Package Manager (fpm):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_8_7_Generic_Elemental_Procedures"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")

[*] The array i is:     1    2    3
[*] The array j is:     7    8    9

[*] Now calling swap on the integer arrays....

[*] After swapping, the array i is:     7    8    9
[*] After swapping, the array j is:     1    2    3

[*] The array a is:   1.1  2.2  3.3
[*] The array b is:   7.7  8.8  9.9

[*] Now calling swap on the real arrays....

[*] After swapping, the array a is:   7.7  8.8  9.9
[*] After swapping, the array b is:   1.1  2.2  3.3