DaPhil
DaPhil

Reputation: 1589

Fortran: external function argument has size 0

I have the following code structure:

PROGRAM main

  IMPLICIT NONE

  REAL*8, DIMENSION(:), ALLOCATABLE :: var

  ALLOCATE(var(3))

  var(1) = 1.0d0
  var(2) = 2.0d0
  var(3) = 3.0d0

  CALL f1(var, f2)

  CONTAINS

  SUBROUTINE f1(arg_in, fun)

    REAL*8, DIMENSION(:), INTENT(IN) :: arg_in
    EXTERNAL fun

    PRINT *, arg_in

    CALL fun(arg_in)

    PRINT *, arg_in

  END SUBROUTINE f1

  SUBROUTINE f2(arg_in)

    REAL*8, DIMENSION(:), INTENT(INOUT) :: arg_in

    PRINT *, SIZE(arg_in)

    arg_in = 2.0d0 * arg_in

    RETURN

  END SUBROUTINE f2

END PROGRAM main

When executing the program I noticed that the size of the array in function f2 is 0. In my real program I have the same problem although I get an error message in contrast to my simple program above:

Attempt to use pointer F when it is not associated with a target

Could anyone give a hint what might be the problem?

Upvotes: 1

Views: 87

Answers (1)

You pass the subroutine as external, but it has an assumed shape dummy argument, therefore it needs an explicit interface, you have to specify one using procedure

procedure(f2) :: fun

or an interface block

interface
  SUBROUTINE fun(arg_in)
    REAL*8, DIMENSION(:), INTENT(INOUT) :: arg_in
  end
end interface

After that you will also realize, that f2 modifies the array arg_in, and therefore it is illegal to reference it from f1, which has the array arg_in as intent(in).

Upvotes: 4

Related Questions