Fortran/Mixing languages

From Wikibooks, open books for an open world
Jump to navigation Jump to search
language extensions Fortran
Mixing languages
Documenting Fortran

Types[edit | edit source]

Fortran types map quite well to intrinsic types in other compiled languages. The following is a table of Fortran-to-C types:

 Fortran            C
 =======            =
 COMMON             extern struct
 INTEGER*1          signed char
 INTEGER*2          short
 INTEGER*4          long
 INTEGER*8          long long
 INTEGER            int
 REAL               float
 REAL*4             float
 REAL*8             double
 REAL*16            long double
 LOGICAL            int
 LOGICAL*n          char [n]
 CHARACTER*n        char [n]
 DOUBLE PRECISION   double
 COMPLEX            float [2]
 COMPLEX*8          float [2]
 COMPLEX*16         double [2]
 COMPLEX*32         long double [2]

Arrays[edit | edit source]

In Fortran, the leftmost array subscript changes the fastest, not the slowest, so the item following x(1,1) is x(2,1), not x(1,2). By default the index of the first element of an array is 1, not 0.

Global Storage[edit | edit source]

See the Common Blocks section.

Subroutine and function calls[edit | edit source]

Many languages push their arguments onto the stack, some as constants and some as addresses. In most compilers, Fortran will compile a block of pointers to variables and constants, and push the address of that block. So, if we had a Fortran procedure defined as follows:

subroutine my_sub(i, j, x)

then the C definition would be:

struct my_sub_args {
    int *i;
    int *j;
    float *x;
} my_sub_args = {&i, &j, &x};
void my_sub(my_sub_args*);

The C code could call the routine as follows:

my_sub(&my_sub_args);

The PL/1 Special Case[edit | edit source]

In PL/1, you can define an external common block, subroutine, or procedure to be of type FORTRAN. When you do this, everything, down to subscript order, will be handled for you. Likewise, you can define a PL/1 item, such as a subroutine, to be of type FORTRAN, and it will then be callable by Fortran using Fortran's calling conventions.