How to pass subroutine names as arguments in Fortran?

It is call action(mySubX) provided action looks as subroutine action(sub) !either – not recommmended, it is old FORTRAN77 style external sub !or – recommended interface subroutine sub(aA, aB) integer,intent(…) :: aA, aB end subroutine end interface ! NOT BOTH!! call sub(argA, argB) provided action knows what to put there as argA, argB to represent aA, … Read more

Pass arrays from C/C++ to Fortran and return a calculated array

Under the rules of current Fortran (Fortran 2008, but this is the same for when C interoperability was introduced in Fortran 2003), a Fortran procedure is not interoperable with C if it has an assumed shape dummy argument (other restrictions also apply). In your code degC, the dummy argument in the function DegCtoF, declared as … Read more

Skip a line from text file in Fortran90

One possible solution has already been presented to you which uses a “dummy variable”, but I just wanted to add that you don’t even need a dummy variable, just a blank read statement before entering the loop is enough: open(18, file=”m3dv.dat”) read(18,*) do … The other answers are correct but this can improve conciseness and … Read more

Difference between intent(out) and intent(inout)

intent(inout) and intent(out) are certainly not the same. You have noted why, although you don’t draw the correct conclusion. On entering the subroutine useless a is undefined, rather than defined. Having a variable “undefined” means that you cannot rely on a specific behaviour when you reference it. You observed that the variable a had a … Read more

STL analogue in Fortran

There are no templates in Fortran and hence no STL. You can try FLIBS for some generic libraries. It generally uses transfer() tricks to achieve generic programming. There is a preprocessor which adds some templates to Fortran and comes with some small STL, you can try that too named PyF95++. If you have access to … Read more

How to get priorly-unknown array as the output of a function in Fortran

I hope a real Fortran programmer comes along, but in the absence of better advice, I would only specify the shape and not the size of x(:), use a temporary array temp(size(x)), and make the output y allocatable. Then after the first pass, allocate(y(j)) and copy the values from the temporary array. But I can’t … Read more

Fortran – Cython Workflow

Here’s a minimum working example. I used gfortran and wrote the compile commands directly into the setup file. gfunc.f90 module gfunc_module implicit none contains subroutine gfunc(x, n, m, a, b, c) double precision, intent(in) :: x integer, intent(in) :: n, m double precision, dimension(n), intent(in) :: a double precision, dimension(m), intent(in) :: b double precision, … Read more