This topic contains 2 replies, has 2 voices, and was last updated by Alessandro Di Nola April 15, 2020 at 8:00 pm.
February 25, 2020 at 6:53 pm #1730
Alessandro Di Nola
in section 2.2.4 of the book (page 58) it is stated that “we can only pass on functions or subroutines to fzero that are stored in a module”. My understanding is that this may be true with old version of Fortran (or compiler specific?) since with ifort storing functions as internal subprograms works fine. In other words, it seems that you don’t need to store functions to be passed to the equation solver in a module. This is a toy example to show the problem. In the main program I use the equation solver FZERO from the toolbox and I store the function as an internal function (no module other than the toolbox is used)
use toolbox, only: fzero
!—— Declaration section ————–!
real(8) :: x0
logical :: check
!—— Execution section —————-!
x0 = 0.5d0
write(*,*) ‘Find the root of f(X)=1/X-1’
write(*,*) ‘Results of fzero: ‘
write(*,'(1X,A,F12.7,A,F12.7)’) ‘x0 = ‘, x0, ‘ f(x0) = ‘,myfunc(x0)
function myfunc(x) result(F)
real(8), intent(in) :: x
real(8) :: F
F = 1.0d0/x – 1.0d0
end function myfunc
end program testMarch 3, 2020 at 1:58 pm #1735
you are totally right. Within the recent developments of the
ifortcompiler, a lot of things have changed. This predominantly has to do with the development of the new Fortran2008 standard.
When we started writing the book and developing all its codes, the rule was still that functions and subroutines that were located within the main program needed and explicit interface if they should have been passed to an external module. However, functions and subroutines stored in a module do automatically get an interface, so that we as programmers don’t have to explicitly write it. This was very convenient.
In the recent developments of the Fortran language, especially with the new object orientation, these conventions have changed and it it now easier to communicate functions and subroutines between programs and modules. So, as you rightly remark, you can now also store a function in a main program and pass it on to the toolbox.
Thanks for pointing it out. In a future edition, we might update our programs and book accordingly.
FabianApril 15, 2020 at 8:00 pm #1753
Alessandro Di Nola
thanks a lot for your clarification!
You must be logged in to reply to this topic.