0
$\begingroup$

For instance, consider a module with the following general structure:

module test
    implicit none

    contains 

    function f(x)
    ...
    end function f


    subroutine sub()
    ...
        y = f(x)
    ...   
    end subroutine


end module test

As such this is incorrect, as is doing something similar inside a program (internal procedures can not reference each other). When I tested it in an module, I still get the same error. Presently I am putting f outside the module, and it works. The reason I am not satisfied with that is I want f to be available to programs that use test; now it is available only to the procedures that are defined inside this module file.

$\endgroup$
3
  • $\begingroup$ This isn't a Computational Science question, but a programming language question. You will be better off asking this on a Fortran-specific site. $\endgroup$ Aug 20, 2021 at 15:30
  • $\begingroup$ This would be an appropriate question for stack overflow, but since this is a question about programming in Fortran and there's no scientific computing content it's not appropriate here. You'll find quite a few Fortran language specific questions on stack overflow. $\endgroup$ Aug 20, 2021 at 15:30
  • $\begingroup$ Thank you. I did that and it was answered. $\endgroup$
    – duality
    Aug 20, 2021 at 19:02

2 Answers 2

1
$\begingroup$

Put two print, *s in that code and test it yourself. That's a very simple question to test.

$\endgroup$
1
  • $\begingroup$ Yes I have tested that and it was not working. I couldn't call f from sub. $\endgroup$
    – duality
    Aug 20, 2021 at 17:29
0
$\begingroup$

There is nothing wrong with your module. And yes, you can certainly call a module procedure from inside another module procedure. This is one of the major reasons for the existence of modules in Fortran since the 1980s: to encapsulate all relevant procedures in one programming unit. But keep in mind that you do not need to use a module procedure to call it in another module procedure. The interfaces of all module procedures are exposed to each other. So this would be wrong:

module test

    implicit none

contains 

    function f(x)
    end function f

    subroutine sub()
        use test, only: f ! This is wrong and there is no need for it
        y = f(x)
    end subroutine

end module test

If you had a minimal working example of your problem that could have helped significantly better pin down the cause of the error. I suspect this error is not about what you think it is as it is totally possible in Fortran.

$\endgroup$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.