2

I have this very small example of Fortran code which I would like to understand please.

  subroutine test_iso_c

  use ISO_C_BINDING

  implicit real(c_double) (a-h,o-z)    

  real :: var, expression

  interface
    real(c_double) function test (y) bind( c )
     use ISO_C_BINDING
       real(c_double), value :: y
     end
  end interface


  ! call 
  var = test(expression) ! - expression  is a real declared variable which is calculated using simple  arithmetic operation.   

  end 

Can you explain to me thee following (I assume c_double means double precision in a C code scope?)

1 - What does implicit real(c_double) (a-h,o-z)?

2 - what does value and bind(c) do in the function interface

3 - I saw this of code as part of a larger routine, can you say what this test function provide/do?

francescalus
  • 30,576
  • 16
  • 61
  • 96
ATK
  • 1,296
  • 10
  • 26
  • 1
    You can read about these details [here](https://stackoverflow.com/tags/fortran-iso-c-binding/info). Please understand that page and consider whether there's more you want to know. As it is, this is rather a broad question. In particular, we can't answer part 3 as we don't know the details of the function `test`. Behaviour of the `implicit` statement here isn't different from any other (pre-F2018) use. – francescalus Feb 11 '19 at 10:00
  • thanks!, What is the pre-F2018 use w.r.t to the `implicit` satement – ATK Feb 11 '19 at 10:18
  • 1
    That `implicit` statement makes any variable with initial letter a, b, c, ..., h, o, p, q, ..., z without an explicit type declaration, of type `real(c_double)`. [This other answer](https://stackoverflow.com/a/49382846/3157076) contains some details of that. – francescalus Feb 11 '19 at 10:22

1 Answers1

0

1 - What does implicit real(c_double) (a-h,o-z)?

The implied type of variables is an old Fortran feature where all variables starting with all characters except i, j, k, l,m, and n are considered to be of type REAL, so there is no need to declare them separately. This property is considered harmful and therefore it is recommended to cancel it by specifying IMPLICIT NONE at the very beginning of the program.

This instruction specifies that all variables beginning with the characters a-h or o-z are of type REAL(c_double), which corresponds to the C double type. The best solution is to use the IMPLICIT NONE directive and then declare all variables like this

REAL(c_double) :: x

2 - what does value and bind(c) do in the function interface

The bind(c) means that the function is C-function, so a compiler must formalize its call in the C language. This includes both the order in which arguments are placed on the stack and the formatting of the function name (for example, adding an underscore to the beginning).

The VALUE attribute means that the parameter is passed to the function by value (usually in Fortran, parameters are passed by reference)