Adjusting Calling Conventions in Mixed-Language Programming

The calling convention determines how a program makes a call to a routine, how the arguments are passed, and how the routines are named (discussed in the section on Adjusting Naming Conventions in Mixed-Language Programming). In a single-language program, calling conventions are nearly always correct, because there is one default for all routines and because header files or Fortran module files with interface blocks enforce consistency between the caller and the called routine.

In a mixed-language program, different languages cannot share the same header files. If, as a result, you link Fortran and C routines that use different calling conventions, the error is not apparent until the bad call is made at run-time. During execution, the bad call causes indeterminate results and/or a fatal error, often somewhere in the program that has no apparent relation to the actual cause: memory/stack corruption due to calling errors. Therefore, you should check carefully the calling conventions for each mixed-language call.

The discussion of calling conventions between languages applies only to external procedures. You cannot call internal procedures from outside the program unit that contains them.

A calling convention affects programming in five ways:

  1. The caller routine uses a calling convention to determine the order in which to pass arguments to another routine; the called routine uses a calling convention to determine the order in which to receive the arguments passed to it. In Fortran, you can specify these conventions in a mixed-language interface with the INTERFACE statement or in a data or function declaration. 32-bit Visual C/C++ and Fortran both pass arguments in order from left to right.
  2. The caller routine and the called routine use a calling convention to determine which of them is responsible for adjusting the stack in order to remove arguments when the execution of the called routine is complete. You can specify these conventions with ATTRIBUTES (cDEC$ ATTRIBUTES compiler directive) options such as C or STDCALL.
  3. The caller routine and the called routine use a calling convention to select the option of passing a variable number of arguments.
  4. The caller routine and the called routine use a calling convention to pass arguments by value (values passed) or by reference (addresses passed). Individual Fortran arguments can also be designated with ATTRIBUTES option VALUE or REFERENCE.
  5. The caller routine and the called routine use a calling convention to establish naming conventions for procedure names. You can establish any procedure name you want, regardless of its Fortran name, with the ALIAS directive (or ATTRIBUTES option ALIAS). This is useful because C anmd Basic are case sensitive, while Fortran is not.

Specific calling-convention issues are discussed in the following sections: