# Module Type Error

**URL:** <https://fortran-lang.discourse.group/t/module-type-error/5796>\
**Category:** Help\
**Created:** [May 13, 2023, 2:52pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796 "2023-05-13T14:52:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pleiades](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pleiades/32/3033_2.png) [@Pleiades](https://fortran-lang.discourse.group/u/Pleiades)\
**Post date:** [May 13, 2023, 2:52pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/1 "2023-05-13T14:52:11Z")

</div>

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.

```fortran
! 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

```fortran
! 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

```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.

```fortran
   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

```

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [May 13, 2023, 2:59pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/2 "2023-05-13T14:59:16Z")

</div>

The error is because you are passing a double precision matrix (`dp` kind) to a function that takes only single precision (`sp` kind) matrices.

Using matching `kind`s would solve your problem.

---

<div class="post-metadata">

**Author:** ![Pleiades](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pleiades/32/3033_2.png) [@Pleiades](https://fortran-lang.discourse.group/u/Pleiades)\
**Post date:** [May 13, 2023, 3:11pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/3 "2023-05-13T15:11:54Z")

</div>

Thanks for the quick reply.

I thought since I had `use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64` and I used `real(sp)` in both files the kinds were the same.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [May 13, 2023, 3:22pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/4 "2023-05-13T15:22:24Z")

</div>

You are correct, you have imported both single and double precision but in the function definition

> [@Pleiades](#):
>
> ```auto
> subroutine print_matrix(mat)
> real(sp), intent(in):: mat(:,:)
> 
> ```

you are defining the input argument `mat` as a single precision matrix.  
Then in your main program you are defining a double precision matrix called `grid`

> [@Pleiades](#):
>
> ```auto
> real(dp) :: grid(10, 10)
> 
> ```

The compiler sees that and raises an error. The simplest thing you can do is change one of the 2 types so that they are both `sp` or `dp`

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [May 13, 2023, 3:22pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/5 "2023-05-13T15:22:47Z")

</div>

You did not use `real(sp)` in both files, you declared

> [@Pleiades](#):
>
> `real(dp) :: grid(10, 10)`

I expect this was just a typo, or an oversight. However, in general, and especially for large programming projects with many separate files, you should declare the precisions or kinds in a single place, and USE those declarations everywhere. Then if/when you want to change precisions later, you change just one parameter statement (or one use statement in your case), and your whole program then works with just a recompilation.

---

<div class="post-metadata">

**Author:** ![Pleiades](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pleiades/32/3033_2.png) [@Pleiades](https://fortran-lang.discourse.group/u/Pleiades)\
**Post date:** [May 13, 2023, 3:25pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/6 "2023-05-13T15:25:22Z")

</div>

You are correct, it was a typo.

And certainly defining it in a single place would be preferably. I wasn’t sure how to do that yet. But I’d like to.

---

<div class="post-metadata">

**Author:** ![Pleiades](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pleiades/32/3033_2.png) [@Pleiades](https://fortran-lang.discourse.group/u/Pleiades)\
**Post date:** [May 13, 2023, 3:25pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/7 "2023-05-13T15:25:58Z")

</div>

doh! I missed that I had put `sp` instead of `dp`. Thanks!

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [May 13, 2023, 4:02pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/8 "2023-05-13T16:02:08Z")

</div>

> [@Pleiades](#):
>
> And certainly defining it in a single place would be preferably. I wasn’t sure how to do that yet. But I’d like to.

There are many ways to do this. One approach for your little test code is

> [@Pleiades](#):
>
> ```auto
> module mymod
> use, intrinsic :: iso_fortran_env, only: wp=>real32
> ! the rest of the module here.
> end module mymod
> 
> program hello
> use mymod, only: wp, printMat => print_matrix
> ...
> real(wp) :: grid(10,10)
> ! the rest of the main program here.
> end program hello
> 
> ```

Then if you want to change KINDs later, you just change that one definition of `wp` in the module, and that will propagate throughout the rest of your code.

Another point is that the module must always be compiled before the main program. You could specify this in the Makefile as

```auto
hello_world.o: mymod.o

```

This looks a little odd because mymod.o isn’t really needed during the hello\_world.f90 compilation, but the mymod.f90 compilation step is necessary, so you can force the correct compilation order by specifying this artificial dependency.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 13, 2023, 8:07pm UTC](https://fortran-lang.discourse.group/t/module-type-error/5796/9 "2023-05-13T20:07:34Z")

</div>

Welcome to the forum. As a side note, you could print the matrix, one element per line, using the  
**format reversion** feature of Fortran, illustrated below.

```auto
program main
implicit none
integer, parameter :: nrow = 2, ncol = 3
real :: x(nrow, ncol)
integer :: i, j
call random_number(x)
do i = 1, ncol
   do j = 1, nrow
      print*, x(j, i)
   end do
end do
print*,"using format reversion:"
print "(f15.6)", x
end program main

```

Sample output:

```auto
  0.243293464    
  0.532455027    
  0.527422190    
  0.477576017    
  0.826467395    
  0.277657449    
 using format reversion:
       0.243293
       0.532455
       0.527422
       0.477576
       0.826467
       0.277657

```
