Suppose I have a subroutine:
subroutine foo(x, Nx)
implicit none
integer, intent(IN) :: x
integer, intent(IN) :: Nx
select case(x)
case (1)
write(*,*) "Minimum value"
case (Nx)
write(*,*) "Maximum value"
case default
write(*,*) "Somewhere in-between"
end select
end subroutine foo
Suppose my driver looks like this:
program main
implicit none
interface
subroutine foo(x,Nx)
integer, intent(IN) :: x
integer, intent(IN) :: Nx
end subroutine foo
end interface
integer, parameter :: Nx = 100
integer :: x
call foo(20, Nx)
end program main
The above program will not compile because in the subroutine, case (Nx)
is invalid. Specifically, ifort 16 gives the following error:
error #6601: In a CASE statement, the case-value must be a constant expression.
In other words, even though Nx is effectively declared as a subroutine constant via intent(IN)
, it needs to be either a literal constant or parameter
of type integer
.
Are there any ways to make the case statement accept Nx
as the constant parameter we know it to be? Is there some way to declare Nx
to be a passed-in parameter
?
I realize that in this simple, short example, an if-then-elseif-else-end block would suffice, but then I wouldn't know the answer to this question. :-)