F77-C: Passing Fortran FUNCTION as an argument for a C Function 
Author Message
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Hello,

I have a fortran FUNCTION f1, a Fortran SUBROUTINE s1 and a C function
c1.

c1 has following prototype:

void c1(double *,double (*fkt)(double,double*));

Now i want to CALL c1 from s1 and pass f1 as the second argument for c1.

Is it possible? If yes, then how should i define c1 interface in
Fortran? And how do i call c1 then?

tia,
schamil



Fri, 04 Jul 2008 03:48:40 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:

> Hello,

> I have a Fortran FUNCTION f1, a Fortran SUBROUTINE s1 and a C function
> c1.

> c1 has following prototype:

> void c1(double *,double (*fkt)(double,double*));

> Now i want to CALL c1 from s1 and pass f1 as the second argument for c1.

> Is it possible? If yes, then how should i define c1 interface in
> Fortran? And how do i call c1 then?

Please specify compilers being used.

Quote:

> tia,
> schamil

--

Gary Scott

Fortran Library:  http://www.fortranlib.com

Support the Original G95 Project:  http://www.g95.org
-OR-
Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

Why are there two?  God only knows.

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford



Fri, 04 Jul 2008 05:28:26 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:

> Please specify compilers being used.

Compaq Visual Fortran 6 and Microsoft Visual C++ 6.0

schamil



Fri, 04 Jul 2008 09:06:26 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:

> I have a Fortran FUNCTION f1, a Fortran SUBROUTINE s1 and a C function
> c1.
> c1 has following prototype:
> void c1(double *,double (*fkt)(double,double*));
> Now i want to CALL c1 from s1 and pass f1 as the second argument for c1.
> Is it possible? If yes, then how should i define c1 interface in
> Fortran? And how do i call c1 then?

Not knowing the compiler, the most common Fortran calling convention for
scalar variables, assumed size arrays, and passing an external function
as an argument is to pass the address, which should agree with the cl
declaration.

Writing f1 to accept a call by value argument may be more difficult.

If instead c1 would pass a pointer as the first argument,

void c1(double *,double (*fkt)(double*,double*));

it would be easier.

Otherwise, if your compiler allows functions with arguments passed
by value, instead of by address, it might work.

-- glen



Fri, 04 Jul 2008 16:50:16 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function
module m

implicit none

   interface
      subroutine c1(dbl, sub)
      !DEC$ATTRIBUTES C, REFERENCE:: c1
      real(8):: dbl
      real(8), external:: sub
      end subroutine
   end interface
=================
contains
=================
subroutine s1

call c1(2.71828d0, f1)

end subroutine s1
=================
real(8) function f1(dbl1, dbl2)
!DEC$ATTRIBUTES C:: f1
real(8):: dbl1
!DEC$ATTRIBUTES REFERENCE:: dbl2
real(8):: dbl2

f1 = dbl1 + dbl2

end function f1
end module m

Notes:
1) The code uses non-standard CVF syntax !DEC$, which makes it rather
easy to do mixed-language programming.
2) !DEC$ATTRIBUTES C implies call-by-value; REFERENCE overrides that
3) If dbl2 is an array, you can omit REFERENCE. Argument passing will
still work though (arrays are always passed by reference).
4) I placed both s1 and f1 in a module; that ensures that they're
"mutually visible" and that the compiler will correctly generate
reference to f1 in s1.

--
Jugoslav
www.xeffort.com

Posting via Google news... grrr...



Fri, 04 Jul 2008 19:31:19 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:

> 4) I placed both s1 and f1 in a module; that ensures that they're
> "mutually visible" and that the compiler will correctly generate
> reference to f1 in s1.

Hmm, is it F77 syntax anyway? I can't find anything about "module" in
the F77 standard...
And w/o module i got following errormsg:
Error: This actual argument must be the name of an external user
function or the name of an intrinsic function.

schamil



Sat, 05 Jul 2008 19:58:59 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:


>> 4) I placed both s1 and f1 in a module; that ensures that they're
>> "mutually visible" and that the compiler will correctly generate
>> reference to f1 in s1.

> Hmm, is it F77 syntax anyway? I can't find anything about "module" in
> the F77 standard...
> And w/o module i got following errormsg:
> Error: This actual argument must be the name of an external user
> function or the name of an intrinsic function.

> schamil

Modules are F90, not F77, but CVF is an F90 compiler, so you can use
modules quite happily. What you're doing is already compiler-dependent,
so I really wouldn't worry about whether or not you're using F77 syntax.

Catherine.



Sat, 05 Jul 2008 22:08:55 GMT  
 F77-C: Passing Fortran FUNCTION as an argument for a C Function

Quote:

> > Hmm, is it F77 syntax anyway? I can't find anything about "module" in
> > the F77 standard...
> > And w/o module i got following errormsg:
> > Error: This actual argument must be the name of an external user
> > function or the name of an intrinsic function.

> > schamil

> Modules are F90, not F77, but CVF is an F90 compiler, so you can use
> modules quite happily. What you're doing is already compiler-dependent,
> so I really wouldn't worry about whether or not you're using F77 syntax.

I missed the "F77" part in the original post, but I doubt it's doable
in F77 anyway. Reading between lines, I assumed that C part was not
changeable, and it's not possible to specify call-by-value in a
portable manner using *any* Fortran standard before F2003. So, F90 +
extensions solution I offered is the simplest one.

If the first argument were a pointer/reference (i.e. C part can be
modified), then the binding could be done in semi-portable manner even
if using "pure" F77. However, CVF has specific calling convention
(stdcall) which must be adjusted either by CVF compiler switch or C++
compiler switch or extensions to make the calls match all around.

--
Jugoslav
http://www.xeffort.com



Mon, 07 Jul 2008 20:58:51 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Passing array valued functions as argument to function.

2. to CS: or not to CS: in F-PC assembler

3. fortran-90, module function passed as argument

4. passing functions with variable number of arguments as procedure arguments

5. VC++ calling fortran function and fortran function calling a c++ function

6. WANTED: F77 textbook for 1st undergraduate CS course

7. WANTED: F77 textbook for 1st undergraduate CS course

8. Calling dll functions which need pointers to be passed as an argument

9. Passing array as argument to function

10. Passing arguments to function: Building tree in APL

11. passing fuctions as arguments to other functions

12. passing formal arguments to scheme lambda functions?

 

 
Powered by phpBB® Forum Software