We will now include our dotProduct function in a module. Create the file dotProductModule.f90:

module dotProduct_module
implicit none
contains

integer function dotProduct(a,b)

integer                   :: c
integer, dimension(2)     :: a, b

c = a(1)*b(1) + a(2)*b(2)

dotProduct = c            !The return value is assigned

return
end function dotProduct

end module dotProduct_module
Fortran

Now compile the module (with the -c switch so as not to generate an executable but rather an object for linking):

$ gfortran -c dotProductModule.f90
Shell session
$ ifort -c dotProductModule.f90
Shell session

Check that dotProductModule.o and dotproduct_module.mod have been created (note, the capital P is converted to lowercase in the .mod file name). Now, create dotProductFromMod.f90 and edit it to contain this:

program dotProductFromMod
use dotProduct_module
implicit none
integer, dimension(2)   :: a, b

write(*,*) 'Enter a''s components separated by a space'
read(*,*) a    !This will read a(1) and a(2)

write(*,*) 'Enter b''s components separated by a space'
read(*,*) b

write(*,*) dotProduct(a,b)

end program dotProductFromMod
Fortran

As indicated, the correct position of the use statement is immediately after the program dotProductFromMod line, prior to all the variable declarations (and even the implicit none line). Now we can compile and link, and then run:

$ gfortran dotProductModule.o dotProductFromMod.f90 -o dotProductFromMod
$ ./dotProductFromMod
Shell session
$ ifort dotProductModule.o dotProductFromMod.f90 -o dotProductFromMod
$ ./dotProductFromMod
Shell session
 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)