Fortran/Mixing languages

From Wikibooks, open books for an open world
< Fortran
Jump to: navigation, search

Types[edit]

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]

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]

See the Common Blocks section.

Subroutine and Function Calls[edit]

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 mySub(i, j, x)

then the C definition would be:

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

The C code could call the routine as follows:

 mySub(&mySubArgs);

The PL/1 Special Case[edit]

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.