Mixed-language programming involves a call from a routine written in one language to a function, procedure, or subroutine written in another language. For example, a Fortran main program may need to execute a specific task that you want to program separately in an assembly-language procedure, or you may need to call an existing DLL or system procedure.
Mixed-language programming is possible with Visual Fortran, Visual C/C++, Visual Basic, and assembly language (MASM) because each language implements functions, subroutines, and procedures in approximately the same way. The following table shows how different kinds of routines from each language correspond to each other. For example, a C main program could call an external void function, which is actually implemented as a Fortran subroutine.
Language Equivalents for Calls to Routines
Language | Call with Return Value | Call with No Return Value |
---|---|---|
Fortran | FUNCTION | SUBROUTINE |
C and Visual C++ | function | (void) function |
Visual Basic | Function | Sub |
Assembly language | Procedure | Procedure |
There are some important differences in the way languages implement routines. Argument passing, naming conventions and other interface issues must be thoughtfully and consistently reconciled between any two languages to prevent program failure and indeterminate results. However, the advantages of mixed-language programming often make the extra effort worthwhile.
A summary of a few mixed-language advantages and restrictions follows:
Assembly-language routines are small and execute very quickly because they do not require initialization as do high-level languages like Fortran and C. Also, they allow access to hardware instructions unavailable to the high-level language user. In a Fortran/assembly-language program, compiling the main routine in Fortran gives the assembly code access to Fortran high-level procedures and library functions, yet allows freedom to tune the assembly-language routines for maximum speed and efficiency. The main program can also be an assembly-language program.
A mix of Fortran and Visual Basic 4.0 or higher (32-bit) allows you to use the easy-to-implement user-interface features of Visual Basic, yet do all your computation, especially floating-point math, in Fortran routines. In a Fortran/Visual Basic program, the main routine must be Visual Basic. It is not possible to call Basic routines from Fortran. You can call Visual Basic routines as callbacks, but the main program must be Visual Basic.
Generally, Fortran/C programs are mixed to allow one to use existing code written in the other language. Either Fortran or C can call the other, so the main routine can be in either language.
To use the same Microsoft visual development environment for multiple languages, you must have the same version of the visual development environment for your languages (see Mixed-Language Development Support in Compaq Visual Fortran Installing and Getting Started).
This section provides an explanation of the keywords, attributes, and techniques you can use to reconcile differences between Fortran and other languages. Adjusting calling conventions, adjusting naming conventions and writing interface procedures are discussed in the next sections:
After establishing a consistent interface between mixed-language procedures, you then need to reconcile any differences in the treatment of individual data types (strings, arrays, and so on). This is discussed in Exchanging and Accessing Data in Mixed-Language Programming .