Passing Arguments in Mixed-Language Programming

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.


Note: In addition to ATTRIBUTES, the compiler option /iface also establishes some default argument passing conventions (such as for hidden length of strings).

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

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.