Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Pass Arguments in Mixed-Language Programming

By default, Fortran programs pass arguments by reference. They pass a pointer to each actual argument rather than the value of the argument. C programs typically pass arguments by value. Consider the following:

  • When a Fortran program calls a C function, the C function's formal arguments must be declared as pointers to the appropriate data type.

  • When a C program calls a Fortran subprogram, each actual argument must be specified explicitly as a pointer.

You can pass data between Fortran and C/C++ using argument lists just as you can within each language (for example, the argument list a, b and c in CALL MYSUB(a,b,c)). You can pass individual arguments in two ways:

  • By value: Passes the argument's value.

  • By reference: Passes the address of the arguments. On Intel® 64 architecture, Fortran, C, and C++ use 8-byte addresses.

You need to make sure that for every call, the calling program and the called routine agree 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).

On Windows, option /iface:cvf changes the way that CHARACTER variables are passed. With /iface:cvf, CHARACTER variables are passed as address/length pairs (/iface:mixed_str_len_arg).