11.1 Making Mixed-Language Calls

Mixed-language programming always involves a call to a function, procedure, or subroutine. For example, a BASIC main module may need to execute a specific task that you would like to program separately. Instead of calling a BASIC subprogram, however, you decide to call a C function.

Mixed-language calls involve calling functions in separate modules. Instead of compiling all of your source modules with the same compiler, you use different compilers. In the instance mentioned above, you would compile the main-module source file with the BASIC compiler, another source file (written in C) with the C compiler, and then link the two object files.

Figure 11.1 illustrates how the syntax of a mixed-language call works, using the instance mentioned above.

In Figure 11.1, the BASIC call to C is CALL Prn, similar to a call to a BASIC subprogram. There are two differences between this mixed-language call and a call between two BASIC modules:

The subprogram Prn is implemented in C, using standard C syntax.

The implementation of the call in BASIC is affected by the DECLARE statement, which uses the CDECL keyword to create compatibility with C. The DECLARE statement (which is described in detail in the Microsoft BASIC Language Reference and the Microsoft BASIC Programmer's Guide) is an example of a mixed-language “interface” statement. These interface statements override default naming and calling conventions. Each language provides its own form of interface.

You can make mixed-language calls to routines regardless of whether they have return values. (In this chapter, “routine” refers to any function, procedure, or subroutine that can be called from another module.)

Table 11.1 shows the correspondence between calls to routines in different languages.

Table 11.1 Language Equivalents for Routine Calls

Language Return Value No Return Value

Assembly Language Procedure Procedure
BASIC FUNCTION procedure Subprogram
C/C++ function (void) function
FORTRAN FUNCTION SUBROUTINE
Pascal Function Procedure

For example, a C module can make a subprogram call to a FORTRAN subroutine. You can prototype a FORTRAN subroutine as a function with a void type.

Note:

BASIC DEF FN functions and GOSUB subroutines cannot be called from another language.