Hello! I’m completely new to Fortran but not to scientific programming nor to compiled languages. I am trying to learn Fortran and playing around with the tutorial at fortran-lang. I’m currently exploring code organization.
I’m trying to implement a module with a single subroutine. Below is the code.
! hello_world.f90
program hello
use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
use mymod, only: printMat => print_matrix
implicit none
integer:: nrow, ncol
integer:: i, j
real(dp) :: grid(10, 10)
nrow = 10
ncol = 10
grid(:,:) = 0.0_dp
call printMat(grid)
end program hello
And my module here
! mymod.f90
module mymod
use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
implicit none
private
public param1, print_matrix
real, parameter :: param1 = 10
contains
subroutine print_matrix(mat)
real(sp), intent(in):: mat(:,:)
integer:: i, j, nrow, ncol
nrow = size(mat, 1)
ncol = size(mat, 2)
do i = 1, ncol
do j = 1, nrow
print *, mat(j, i)
end do
end do
end subroutine print_matrix
end module mymod
And finally my makefile
FC=gfortran
FFLAGS= -Wall
main: mymod.o hello_world.o
$(FC) $(FFLAGS) $^ -o main
hello_world.o: hello_world.f90
$(FC) -c $(FFLAGS) $<
mymod.o: mymod.f90
$(FC) -c $(FFLAGS) $<
clean:
rm main *.mod *.o
When I try to compile this I get the following error. I’m not sure what the problem is - I’d appreciate any ideas on what I did wrong here.
17 | call printMat(grid)
| 1
Error: Type mismatch in argument ‘mat’ at (1); passed REAL(8) to REAL(4)
make: *** [makefile:8: hello_world.o] Error 1