Silverfrost Forums

Welcome to our forums

problems array valued functions

20 Jun 2015 1:35 #16480

Dear all

I have found several problems with array valued functions. They are probably my fault. Nevertheless I think the diagnosis of the compiler may be better.

This is the first example. It fails at runtime if compiled with /CHECKMATE depending on whether an unused variable N is passed to the function via USE or not

   module global
      integer::N=1
      interface fun
       function fun(A,B)
        REAL,intent(in)::A(:),B(:)
        REAL fun(size(A),size(B))
       end function fun
     end interface
     end module

     program bug6
     use global,only:fun     
     REAL A(4),B(4,4)
     A=(/1.,2.,3.,4./)
     B=fun(A,A)
     PRINT*,B
     end program

     function fun(A,B)
     use global,only:N   ! Crashes (integer overflow) due to this line if 
                                 !compiled with /CHECKMATE !!!!
     implicit none
     REAL,intent(in)::A(:),B(:)
     REAL fun(size(A),size(B))
     fun=5.                ! the error message points to this line
     end function fun

Probably the reference at N should be included in the interface too. In fact, this versión does not fail:

     module Nmodule
     integer::N=1
     end module
     
     module global
      interface fun
       function fun(A,B)
        use Nmodule
        REAL,intent(in)::A(:),B(:)
        REAL fun(size(A),size(B))
       end function fun
     end interface
     end module

     program bug6fixed
     use global,only:fun     
     REAL A(4),B(4,4)
     A=(/1.,2.,3.,4./)
     B=fun(A,A)
     PRINT*,B
     end program

     function fun(A,B)
     use Nmodule
     implicit none
     REAL,intent(in)::A(:),B(:)
     REAL fun(size(A),size(B))
     fun=5.
     end function fun

Regards

10 Jul 2015 7:54 #16569

Is the following what you wanted ? module global integer::N=1 contains function fun(A,B) implicit none REAL,intent(in)::A(:),B(:) REAL fun(size(A),size(B)) fun=5. ! the error message points to this line end function fun
end module global

      program bug6 
      use global,only:fun      
      REAL A(4),B(4,4) 
      A=(/1.,2.,3.,4./) 
      B=fun(A,A) 
      PRINT*,B 
      end program 

I would get the program working, before applying ',only' to the use statement. Why the reference to N ?

Please login to reply.