I am a bit perplexed by this one, it seems to me that assumed-size dummy arguments are actually treated as rank 1 if the subprogram is in a interface block such as:
randrng subroutine with 2 different interfaces in a module
module min_random
use, intrinsic :: iso_fortran_env, only: sp => real32
implicit none
private
interface randrng
module procedure randrng_sp
end interface randrng
public :: randrng
public :: randrng_sp
contains
subroutine randrng_sp(val, n, val_min, val_max)
implicit none
real(sp), intent(out) :: val(*)
integer, intent(in) :: n
real(sp), intent(in) :: val_min, val_max
call random_number(val(1:n))
val(1:n) = val(1:n)*(val_max - val_min) + val_min
end subroutine randrng_sp
end module min_random
When I try to write code that calls this function, if I call it as call randrng
, then my first argument x
seemingly must be an array of rank 1. Otherwise, gfortran, ifort, and ifx all produce errors such as:
ifort compiler error message
app/main.f90(12): error #6285: There is no matching specific subroutine for this generic subroutine call. [RANDRNG]
call randrng(x, size(x), x_min, x_max)
---------^
app/main.f90(16): catastrophic error: Too many errors, exiting
compilation aborted for app/main.f90 (code 1)
The above error is from ifort, but gfortran and ifx say similar.
However, if I call the routine as call randrng_sp
then I can supply x
of any shape I want, and the first n
elements will be filled as originally intended. Is this the way modules and interfaces are meant to work, or why does this happen?
EDIT: For completeness
This program will not compile with gfortran 11.3.0, ifort 2021.9.0, or ifx 2023.1.0
program main
use min_random
implicit none
integer, parameter :: n=2
real :: x(n,n), x_min=0.0, x_max=100.0
call randrng(x, size(x), x_min, x_max)
write(*,*) 'x: ',x
end program main
This program will compile and run as expected.
program main
use min_random
implicit none
integer, parameter :: n=2
real :: x(n,n), x_min=0.0, x_max=100.0
call randrng_sp(x, size(x), x_min, x_max)
write(*,*) 'x: ',x
end program main