Passing Arguments in Mixed-Language Programming

You can pass data between Fortran, C, and C++ 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 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 option, all arguments by default are passed by reference.

In Fortran, in addition to establishing argument passing with the calling-convention option of C, 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 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 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 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 testproc_( int ValParm, int *RefParm );

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 option

Scalars and derived types

REFERENCE option

Default

Default

Arrays

Default

Cannot pass by value

C option

Arrays

Default

Cannot pass by value

C/C++

Non-arrays

Pointer argument_name

Default

Arrays

Default

Struct {type} array_name

This table does not describe argument passing of strings and Fortran 95/90 pointer arguments in Intel 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 -mixed_str_len_arg (immediately after the address of the beginning of the string) or -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.