12.07.2015 Views

PGI User's Guide

PGI User's Guide

PGI User's Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12. Inter-language Callingextern "C" struct {int i;struct {float real, imag;} c;struct {double real, imag;} cd;double d;} com_;TipFor global or external data sharing, extern "C" is not required.Argument Passing and Return ValuesIn Fortran, arguments are passed by reference, that is, the address of the argument is passed, rather than theargument itself. In C/C++, arguments are passed by value, except for strings and arrays, which are passedby reference. Due to the flexibility provided in C/C++, you can work around these differences. Solving theparameter passing differences generally involves intelligent use of the & and * operators in argument passingwhen C/C++ calls Fortran and in argument declarations when Fortran calls C/C++.For strings declared in Fortran as type CHARACTER, an argument representing the length of the string is alsopassed to a calling function.On Linux and Mac OS X systems, or when using the UNIX calling convention on Windows (option -Miface=unix), the compiler places the length argument(s) at the end of the parameter list, following theother formal arguments. The length argument is passed by value, not by reference.Passing by Value (%VAL)When passing parameters from a Fortran subprogram to a C/C++ function, it is possible to pass by value usingthe %VAL function. If you enclose a Fortran parameter with %VAL(), the parameter is passed by value. Forexample, the following call passes the integer i and the logical bvar by value.integer*1 ilogical*1 bvarcall cvalue (%VAL(i), %VAL(bvar))Character Return Values“Functions and Subroutines,” on page 152 describes the general rules for return values for C/C++ andFortran inter-language calling. There is a special return value to consider. When a Fortran function returns acharacter, two arguments need to be added at the beginning of the C/C++ calling function’s argument list:• The address of the return character or characters• The length of the return characterExample 12.1, “Character Return Parameters” illustrates the extra parameters, tmp and 10, supplied by thecaller:155

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!