You can pass data between Fortran and C, Visual C++, Visual Basic, and MASM through calling argument lists just as you can within each language (for example, the argument list a, b and c in CALL MYSUB(a,b,c)). There are two ways to pass individual arguments:
You need to make sure that for every call, the calling program and the called routine agree on how each argument is passed. Otherwise, the called routine receives bad data.
The Fortran technique for passing arguments changes depending on the calling convention specified. By default, Fortran passes all data by reference (except the hidden length argument of strings, which is passed by value).
If the ATTRIBUTES C or STDCALL option is used, the default changes to passing all data by value except arrays. If the procedure has the REFERENCE option as well as the C or STDCALL option, all arguments by default are passed by reference.
In Fortran, in addition to establishing argument passing with the calling-convention options C and STDCALL, you can specify argument options, VALUE and REFERENCE, to pass arguments by value or by reference. In mixed-language programming, it is a good idea to specify the passing technique explicitly rather than relying on defaults.
Examples of passing by reference and value for C, Visual Basic and MASM
follow. All are interfaces to the example Fortran subroutine TESTPROC
below.
The definition of TESTPROC
declares how each argument is passed. The REFERENCE
option is not strictly necessary in this example, but using it makes the argument's
passing convention conspicuous.
SUBROUTINE TESTPROC( VALPARM, REFPARM )
!DEC$ ATTRIBUTES VALUE :: VALPARM
!DEC$ ATTRIBUTES REFERENCE :: REFPARM
INTEGER VALPARM
INTEGER REFPARM
END SUBROUTINE
In C and Visual C++ all arguments are passed by value, except arrays, which are
passed by reference to the address of the first member of the array. Unlike Fortran,
C and Visual C++ do not have calling-convention directives to affect the way individual
arguments are passed. To pass non-array C data by reference, you must pass a pointer
to it. To pass a C array by
value, you must declare it as a member of a structure and pass the structure. The
following C declaration sets up a call to the example Fortran TESTPROC
subroutine:
extern void __stdcall TESTPROC( int ValParm, int *RefParm );
In Visual Basic, arguments are passed by reference by default. To pass arguments by value, you use the keyword BYVAL in front of the argument in the DECLARE statement. For example:
Declare Sub TESTPROC Lib "C:\f90\TESTPROC.DLL"
(ByVal Valparm As Long, Refparm As Long)
Strings are a special case (see the discussion on character strings in Handling Character Strings).
Arrays of numbers require special handling (see data type considerations in Calling Visual Fortran from Visual Basic.
In MASM, arguments are passed by value by default. Arguments to be passed by reference are designated with PTR in the PROTO and PROC directives. For example:
TESTPROC PROTO STDCALL, valparm: SDWORD, refparm: PTR SDWORD
To use an argument passed by value, use the value of the variable. For example:
mov eax, valparm ; Load value of argument
This statement places the value of valparm into the EAX register.
To use an argument passed by reference, use the address of the variable. For example:
mov ecx, refparm ; Load address of argument
mov eax, [ecx] ; Load value of argument
These statements place the value of refparm into the EAX register.
The following table summarizes how to pass arguments by reference and value. An array name in C is equated to its starting address because arrays are normally passed by reference. You can assign the REFERENCE property to a procedure, as well as to individual arguments.
Passing Arguments by Reference and Value
Language | ATTRIBUTE | Argument Type | To Pass by Reference | To Pass by Value |
---|---|---|---|---|
Fortran | Default | Scalars and derived types | Default | VALUE option |
C or STDCALL option | Scalars and derived types | REFERENCE option | Default | |
Default | Arrays | Default | Cannot pass by value | |
C or STDCALL option | Arrays | Default | Cannot pass by value | |
Visual C/C++ | Non-arrays | Pointer argument_name | Default | |
Arrays | Default | Struct {type} array_name | ||
Visual Basic | All types | Default | ByVal | |
Assembler (ia32) MASM | All types | PTR | Default |
This table does not describe argument passing of strings and Fortran 95/90 pointer arguments in Visual Fortran, which are constructed differently than other arguments. By default, Fortran passes strings by reference along with the string length. String length placement depends on whether the compiler option /iface:mixed_str_len_arg (immediately after the address of the beginning of the string) or /iface:nomixed_str_len_arg (after all arguments) is set.
Fortran 95/90 array pointers and assumed-shape arrays are passed by passing the address of the array descriptor.
For a discussion of the effect of attributes on passing Fortran 95/90 pointers and strings, see Handling Fortran 90 Pointers and Allocatable Arrays and Handling Character Strings.