[Rd] Calling FORTRAN function from R issue?

Berend Hasselman bhh at xs4all.nl
Tue Mar 6 15:07:39 CET 2012

On 06-03-2012, at 14:37, Berwin A Turlach wrote:

> G'day Berend,
> On Tue, 6 Mar 2012 13:06:34 +0100
> Berend Hasselman <bhh at xs4all.nl> wrote:
> [... big snip ...]
>> But I would really like to hear from an Rexpert why you
>> shouldn't/can't use external here in the Fortran.
> Probably less a question for an Rexpert but for a Fortran expert....
> If you insert "implicit none" (one of my favourite extensions that I
> always use) as the first statement after 
> 	subroutine callzdotc(retval,n, zx, incx, zy, incy)
> you will see what is going on.  The compiler should refuse compilation
> and complain that the type of zdotc was not declared (at least my
> compiler does).  For FORTRAN to know that zdotc returns a double
> complex you need the 
> 	double complex zdotc
> declaration in callzdotc.

I usually use -fimplicit-none or a similar option as argument for a fortran compiler.
I didn't use it in a syntax checking pre compiling run.

> An
> 	external double complex zdotc
> would be necessary if you were to call another subroutine/function, say
> foo, that accepts functions as formal arguments and you want to pass
> zdotc via such an argument.  Something like
> 	subroutine callzdotc(....)
>        ....
>        external double complex zdotc
> 	....
>        call foo(a, b, zdotc)
>        ....
> 	return
> 	end
> 	subroutine(a, b, h)
> 	double complex h, t
> 	....
> 	t = h(..,..,..,..)
> 	....
> 	return
> 	end
> In C, the qualifier (or whatever the technical term is) "external" is
> used to indicate that the function/variable/symbol is defined in
> another compilation unit.  In FORTRAN77, "external" is used to tell the
> compiler that you are passing a function to another
> function/subroutine.  At least that is my understanding from what I
> re-read in my FORTRAN documentation.

Not quite true.
It is required to use external if you are passing the name of a function/subroutine to another routine.
Otherwise it is just a statement telling the compile that the <external-name> is external to the function/subroutine/.. being compiled.

See http://www.fortran.com/F77_std/rjcnf0001-sh-8.html#sh-8.7

> Thus, perhaps strangely, if there is only a 
> 	external double complex zdotc
> declaration in your subroutine, the compiler doesn't know that a call
> ....

If I am reading the above reference correctly, the syntax 

	external double complex zdotc

is simply incorrect (correct syntax uses commas to separate names e.g. A,B,C).
The compiler (gfortran) simply seems to ignore the "double" and the "complex" if you don't use implicit none.


More information about the R-devel mailing list