A part of a code I have has constants defined inside the module under consideration. Here's what I am talking about:
real(RealExt), parameter :: grav_acc = 9.80665
real(RealExt), parameter :: r_gas_dry = 287.026
real(RealExt), parameter :: cp_air_dry = 1.005e+03
real(RealExt), parameter :: pi = 4.0*atan(1.0)
real(RealExt), parameter :: seconds_per_day = 8.6400e+04
I am basically trying to wrap this fortran code, and use cffi to interface it with python so that when I give inputs in python, the fortran code receives it and does stuff as required. The problem for me is not with cffi, but with the idea of parameters.
The type RealExt is defined as follows:
INTEGER, PARAMETER :: RealExt=SELECTED_REAL_KIND(15, 307)
These constants are in a subroutine; my goal is to send the values of these constants (like gravity, rotation rate of the planet etc) to this subroutine and have them treated as constants for the calculations that come later. But, as far as I have read on this site, parameter attribute requires for the values to be known at runtime. So, I can't just do
subroutine runes(g, rd, cp_rd, sec_per_day)
implicit none
real(RealExt), intent(in) :: g, rd, cp_rd, pi_, sec_per_day
real(RealExt), parameter :: grav_acc = g
real(RealExt), parameter :: r_gas_dry = rd
real(RealExt), parameter :: cp_air_dry = cp_rd
real(RealExt), parameter :: pi = pi_
real(RealExt), parameter :: seconds_per_day = sec_per_day
.
.
.
Is there a work-around for this? Can I somehow declare values passed into the subroutine as constants?
P.S: Please let me know if I have to clarify some of my doubts, if not clear enough.
intent(in)and the compiler will disallow code to modify them inside the routine. But I guess you know that already, so I'm not entirely sure I understand what kind of answer you seek.Parameters aren't just variables that can't change value during the life time of the program. They are fundamentally different, they are constants like 2 or pi whose value can never, ever change. If you want to vary the value of something you need to make it a variable! As @HighPerformanceMark points out you can protect a variable from changing through parts of the program, but that's not the same as it being a constant.