@M. SB's answer is great if runtime initialization is acceptable to you. If you really need the Fortran parameter , which is set at compile time, you can do it like this:
program main implicit none real, parameter :: a = 1.0 real :: res res = func() write(*,*) res contains function func() real, parameter :: b = a + 1.0 real :: func func = b end function func end program main
I suspect part of the confusion is due to differences in language. Often a βparameterβ is used to indicate a function argument, but in Fortran it is never used that way. Instead, it means something similar to const in C / C ++. So, it is not clear to me from your question whether you really need the Fortran parameter or not.
In my example above, the parameter a is known inside func through the host association, which is Fortran lingo for nested areas. You can also do this with modules using a usage association, but this is a bit more verbose:
module mypars implicit none real, parameter :: a = 1.0 end module mypars module myfuncs implicit none contains function func() use mypars, only: a real, parameter :: b = a + 1.0 real :: func func = b end function func end module myfuncs program main use myfuncs, only: func implicit none real :: res res = func() print *, res end program main
source share