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

What does the function declaration “sub function($$)” mean?

It is a function with a prototype that takes two scalar arguments. There are strong arguments for not actually using Perl prototypes in general – as noted in the comments below. The strongest argument is probably: Far More Than Everything You’ve Ever Wanted to Know about Prototypes in Perl There’s a discussion on StackOverflow from … Read more

Does Fortran preserve the value of internal variables through function and subroutine calls?

To answer your question: Yes Fortran does preserve the value of internal variables through function and subroutine calls. Under certain conditions … If you declare an internal variable with the SAVE attribute it’s value is saved from one call to the next. This is, of course, useful in some cases. However, your question is a … Read more

How to alias a function name in Fortran

Yes, Fortran has procedure pointers, so you can in effect alias a function name. Here is a code example which assigns to the function pointer “f_ptr” one function or the other. Thereafter the program can use “f_ptr” and the selected function will be invoked. module ExampleFuncs implicit none contains function f1 (x) real :: f1 … Read more

Correct use of modules, subroutines and functions in Fortran

Modules are always the right thing to use 😉 If you have a very simple F90 program you can include functions and subroutines in the ‘contains’ block: program simple implicit none integer :: x, y x = … y = myfunc(x) contains function myfunc(x) result(y) implicit none integer, intent(in) :: x integer :: y … … Read more