"Incompatible ranks" when calling external function inside module

Hello community,

while trying to implement a function LA_func that returns an array, I am getting the error:
Error: Incompatible ranks 0 and 1 in assignment at (1)

My function is inside a separate file and I managed to compile it with gfortran, so I can call it from another fortran file. It looks liks this:

module procedures
  implicit none

contains


!-----LA_func----------------------------------------------------
!
!  Function to compute the area and perimeter of a circle of given radius
!
!----------------------------------------------------------------------
FUNCTION LA_func(r)

IMPLICIT NONE
REAL :: LA_func(2)
REAL, INTENT(IN) :: r

! Declare local constant Pi
REAL, PARAMETER :: Pi = 3.1415927

LA_func = [Pi * r * r, 2 * Pi * r]

END FUNCTION LA_func

end module

The function is already declared to be of size (2), correct? What am I missing? Thanks, all the best

1 Like

I have added a main program in the same file:

    program la_main
    use procedures

    REAL, dimension(2) :: results

    results = LA_func(1.0)
    print *, results

    end program la_main

It compiles and runs:

$ gfortran essai.f90 && ./a.out
   3.14159274       6.28318548    
$ gfortran --version
GNU Fortran (Ubuntu 9.3.0-10ubuntu2) 9.3.0

I don’t see any problem with the source you provided. I am confused as to what you did that got the error, since you said you managed to compile it. My guess is that the source with the error has something you haven’t shown us.

1 Like

Thank you both vmagnin and sblionel for your remarks. Actually I am trying to call this function from within the numerical bifurcation software AUTO-07p. There, I only have access to one .f90 file where subroutines are placed…and this is where I write the use procedures
It seems the software picks these subroutines and finds the “use” command there…instead of at the beginning of the software code (?)

I am trying it now on windows on a dummy file, where I just compiled the suggestion of vmagnin with gfortran. How can one run the .o file that resulted from the compilation? I guess from the command terminal?

I think I got it, I have to compile first the module:
gfortran -c procedures.f90

and afterwards the main program, but mentioning every module I need:
gfortran -o program program.f90 procedures.o

gfortran procedures.f90 program.f90 should do the job (the order matters).

1 Like

Welcome @lanast and @sblionel! I’m happy to see you both here.

Yes - you must first compile sources that are modules, and in an order so that you don’t compile a source that USEs a module before that module is compiled. Some compilers will let you get away with a module that occurs later in the same source file, but others won’t, so it’s a good practice to keep the modules separate and compile them first.

When you compile a module, it creates a file (usually with a .mod file type, but that can vary.) The compiler typically looks for these in the same directory it looks for INCLUDE files. They’ll also generate object (.o) files that have to be named in the command that builds the executable, as you found.

2 Likes