Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Getting error when invoking dummy function when it returns an array

Behrooz_F_
Beginner
558 Views

I am trying to invoke an external function through a dummy function name. The error is:

Error    1     error #6634: The shape matching rules of actual arguments and dummy arguments have been violated.     

the code is:

Module DummyFG

contains
    function DF(DummyF, x,n)
    
      implicit none
      integer :: n
      real(kind=8) ,intent(in):: x(n)
      real(kind=8) :: DF
      real (kind=8) :: DummyF
      
      
      DF = DummyF(x,n)
    end function DF

    function DG(DummyG, x, n)
      implicit none
      integer :: n
      real(kind=8) , intent(in):: x(n)
      real(kind=8) :: dG(n)
      real (kind=8), external :: DummyG 
      dg= DummyG(x,n)
    end function DG

end Module DummyFG


!    
   function f(x,n)  !result(f1)
        implicit none
        real(kind=8) :: f
        real(kind=8) ,intent(in) :: x(n)
        integer :: n
        f=(1.0-x(1))**2+100.0*((x(2)-x(1)**2))**2;
    end function f
    
   function G(x,n)  result(g1)
        implicit none
        integer :: n        
        real(kind=8) ,intent(in) :: x(n)
        real(kind=8) :: g1(n)
        g1(1)=-2.0*(1.0-x(1))-400.0*x(1)*(x(2)-x(1)**2)
        g1(2)= 200.0*(x(2)-x(1)**2)
    end function g

The driver routine is:

 program Test
    
    implicit none
   

    ! Variables
    integer, parameter :: n=2
   real(kind=8) :: x(n)=(/2.0,1.0/)
   call check(x,n)

   contains

   subroutine check(x,n)
   use dummyfg
   implicit none

    interface
    function f(x,n)  
        implicit none
        integer :: n
        real(kind=8) :: f
        real(kind=8) ,intent(in) :: x(:)
        end function f

     function g(x,n) result(g1)
        implicit none
        integer :: n
        real(kind=8) ,intent(in) :: x(n)
        real(kind=8) :: g1(n)
     end function g
    end interface
   
   
   integer :: n
   real(kind=8) :: x(n),fc, gc(size(x))
   
  
   fc=df(f,x,n)
   gc=dg(g,x,n)

   end subroutine check


    end program Test

Any help is appreciated very much!!!

 

0 Kudos
2 Replies
mecej4
Honored Contributor III
558 Views

The interface that you provided in Subroutine Check contains an inconsistent declaration, "real(kind=8) ,intent(in) :: x(:)" -- inconsistent because in the code for f(x,n) you have "real(kind=8) ,intent(in) :: x(n)". Similarly, the declaration "real (kind=8), external :: DummyG " in the code for function DG is incorrect. The actual argument for DummyG is a function that returns not a single real(kind=8), but an array of that type. Therefore, an explicit interface for DummyG is needed and you have not provided one.

Consider declaring CONTAINed functions in modules and USE those modules, in order to avoid the risk of providing multiple and inconsistent interfaces. Better yet, when you write subprograms that take one or more procedure arguments, provide abstract interfaces to those  procedures in a module, USE that module to make the interfaces available, and declare the dummy procedure arguments using something along the lines of "procedure(dgproc) :: DummyG".

I have attached a modified version of your code with just enough fixed that it works correctly. However, it is better to change the design along the lines that I stated above.

 

0 Kudos
Behrooz_F_
Beginner
558 Views

mecej4,

Thanks for your quick and insightful response. I appreciate it very much!

0 Kudos
Reply