0

I just wanted to understand how functions, modules, .. work. So I played around a little and I apparently make I mistake when I want to allocate a matrix:

Module test
    implicit none
    real, allocatable, dimension(:,:) :: A
End module test

Program Functiontest
    use test
    implicit none
    real :: testits
    real, allocatable, dimension(:,:) :: Ausgabe
    integer ::l=1, ierror=0
    real, allocatable, dimension(:,:) :: B
    Ausgabe=testits(l)
    print *, 'ausgabe= ', Ausgabe

                    if (.not. allocated(B)) then
                            allocate(B(3,5), STAT=ierror)
                            print *, 'allocate 2'
                    end if
                    if (ierror.neqv.0) then
                            print *, 'does not work either', B
                            stop
                    end if


 End program functiontest


 Function testits(l) result(B)
     Use test
     implicit none
     integer, intent(in) :: l
     integer :: ierror=0
     real, allocatable, dimension(:,:) :: B
     allocate(A(3,5))
     A=0.0e0
     !if (.not. allocated(B)) then 
     !       allocate(B(3,5), STAT=ierror)
     !               print *, 'allocate'
     !       end if
     !       if (ierror.neqv.0) then
     !               print *, 'does not work'
     !               stop
     !       end if
     B=A
 End function testits

Now, if I want to allocate the matrix within the function 'testits', the program stops. But if I want to allocate the matrix B in the program, it works. I don't get why it works within the program but not within the function. I declared the variables and all the necessary stuff in the same way, so why should there be any difference?

anonymous
  • 409
  • 1
  • 4
  • 14
  • gfortran doesnt even compile, ifort runs without a problem.. When you say "it does not work", do you mean the program prints 'does not work' ? – Fl.pf. Nov 10 '16 at 09:12
  • 2
    The function `testits` [must have](https://stackoverflow.com/a/31630662/) an explicit interface available to the caller. Here it hasn't. – francescalus Nov 10 '16 at 09:15
  • Doen't say "it doesn't work", this phrase doesn't tell anything. It does not belong to good questions. Any error messages? Which messages? What is the output of the program? – Vladimir F Героям слава Nov 10 '16 at 09:24
  • Actually, this one is a better duplicate http://stackoverflow.com/questions/22520834/array-valued-function because every array valued function must have explicit interface (not just that returning an allocatable array like here). Should I reopen because of this? – Vladimir F Героям слава Nov 10 '16 at 09:37
  • 1
    Francescalus is right, but make your life easy, use best practice and just provide an interface for EVERY subprogram you write. And the simplest way is to stick every subprogram you write an an appropriate module. You use implicit none for your variables, why don't you want to declare your subprograms? – Ian Bush Nov 10 '16 at 10:03
  • I'm sorry, I should have clarified what I mean with it 'does not work'. I mean that If I allocate the Array in the fuction (the part of the program which is marked as comment) and I want to print the Matrix B/Ausgabe (B is returned by the function in the Array 'ausgabe'), the Matrix is not allocated and as indicated the Phrase 'does not work' is printed. But if I allocate the Array in the same way in the man program, it works perfectly. – anonymous Nov 10 '16 at 16:27
  • By the way, I use ifort as Compiler. I don't want to use an Interface, since I want to understand why it does not work in this specific example – anonymous Nov 10 '16 at 16:27
  • As others have said, it does not work **because** an explicit interface is required for the function in any scope that references the function, as that function has an array result and/or allocatable result. Current versions of ifort will tell you that if you compile with the appropriate diagnostic options (e.g. `/warn all` on windows, or `-warn all` on linux). Without an explicit interface the compiler does not know how to set up the call to the function correctly. – IanH Nov 10 '16 at 18:45
  • ok, thanks! That's the answer that I was looking for :) – anonymous Nov 10 '16 at 19:22

0 Answers0