Pass functions to fzero

Forums Feedback, suggestions, wishes, etc. Pass functions to fzero

Tagged: ,

This topic contains 2 replies, has 2 voices, and was last updated by Alessandro Di Nola April 15, 2020 at 8:00 pm.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #1730

    Alessandro Di Nola

    Dear users,
    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)

    `program test
    use toolbox, only: fzero

    !—— Declaration section ————–!
    implicit none
    real(8) :: x0
    logical :: check

    !—— Execution section —————-!

    x0 = 0.5d0

    call fzero(x0,myfunc,check)

    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)
    implicit none
    real(8), intent(in) :: x
    real(8) :: F

    F = 1.0d0/x – 1.0d0

    end function myfunc

    end program test


    Fabian Kindermann

    Dear Allesandro,

    you are totally right. Within the recent developments of the gfortran and the ifort compiler, 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.



    Alessandro Di Nola

    Dear Fabian,
    thanks a lot for your clarification!


Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.