This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: variable-length arguments in an external function


Dear Arjen,

Thanks for the help! I understand the problem now, and fixed it in my
code. Future questions will go to comp.lang.fortran.

Best,
Daan

On Mon, Jun 1, 2015 at 2:42 PM, Arjen Markus <arjen.markus895@gmail.com> wrote:
> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]