For Programmers: Free Programming Magazines  


Home > Archive > Fortran > January 2006 > F77-C: Passing Fortran FUNCTION as an argument for a C Function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2006-01-15, 7:12 pm

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
Gary L. Scott

2006-01-15, 7:12 pm

Schamil Wackenhut wrote:
> 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.

>
> tia,
> schamil



--

Gary Scott
mailto:garyscott@ev1.net

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
Schamil Wackenhut

2006-01-15, 7:12 pm

Gary L. Scott wrote:

> Please specify compilers being used.


Compaq Visual Fortran 6 and Microsoft Visual C++ 6.0

schamil
glen herrmannsfeldt

2006-01-16, 3:57 am

Schamil Wackenhut wrote:


> 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

Jugoslav Dujic

2006-01-16, 7:58 am

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...

Schamil Wackenhut

2006-01-17, 8:01 am

Jugoslav Dujic wrote:

> 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
Catherine Rees Lay

2006-01-17, 7:05 pm

Schamil Wackenhut wrote:
> Jugoslav Dujic wrote:
>
>
> 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.
Jugoslav Dujic

2006-01-19, 8:02 am


Catherine Rees Lay wrote:

>
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com