variable-length arguments in an external function
Arjen Markus
arjen.markus895@gmail.com
Mon Jun 1 12:42:00 GMT 2015
Hi Daan,
this is not a question about the gfortran compiler, but rather
something about the Fortran language in general. Better post this on
comp.lang.fortran.
However, the thing you run into is that the function f as used in
test_external is NOT the same thing as the function f defined in the
module. The way you have specified it as "real(kind=8), external :: f"
means that the compiler only knows that it may expect a function
returning a real(kind=8) result. More specifically: there will be no
size information on the array.
To get what you want use:
interface
real(kind=8) function f( extra_args )
real(kind=8), dimension(:) :: extra_args
end function f
end interface
instead of the "external" statement.
You could have used a different name than "f" here.
Regards,
Arjen
2015-06-01 14:33 GMT+02:00 Daan van Vugt <daanvanvugt@gmail.com>:
> Hi all,
>
> I'm new to fortran and I'm having trouble understanding the behaviour
> of the following program. It seems that passing an array to an
> external function results in odd behaviour.
> I would expect the code attached below to return 2.0 and 2.0, instead
> of 0.0 and 2.0.
>
> Could anyone point me to the cause and a possible solution of this? Is
> it related to the implementation or have I done something illegal in
> fortran (in which case I wonder why the gfortran did not warn me)
>
> Best,
> Daan van Vugt
>
>
>
>
> module test
> contains
> subroutine test_external(f, extra_args)
> implicit none
> real(kind=8), external :: f
> real(kind=8), intent(in), dimension(:) :: extra_args
>
> write(*,*) f(extra_args)
> end subroutine test_external
>
> real(kind=8) function f(args)
> implicit none
> real(kind=8), intent(in), dimension(:) :: args
> f = sum(args)
> end function f
> end module test
>
> program external_test
> use test
> implicit none
> call test_external(f, (/1.0,1.0/))
> write(*,*) f((/1.0,1.0/))
> end
More information about the Fortran
mailing list