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

Array declaration in Module

mo7mae
Beginner
2,891 Views
Hello,

I wrote a subroutine in Fortran 90 which is being called from a C# programm. This subroutine again calls other subroutines and functions.
Now I'm tyring to declare scalar variables and arrays in a module in order to minimize the writing effort which comes with declaring them in each subroutine/function I write.
I tried it like this:

[bash]module mod

   integer :: var1

   real(8) :: var2

   integer, dimension(var1) :: var3

end module


subroutine bla(var1,var2,var3) !Subroutine, which is called from outside

    use mod

    implicit none

    !program...

    call bla2(var2)

end subroutine bla


real function bla2(var2)

    use mod

    implicit none

   !program...

    return

end real[/bash]


Well, I get quite a few errors. The first ones are

Error 1 error #7940: The initialization shall not appear for this object name.
Error 2 error #6219: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association
Error 3 error #6841: An automatic object must not appear in the specification part of a module. [KNOTID]


I believe that this might be caused from the array declaration in the module. What do I need to change?
I could declare the arrays as allocatable but then I still have to allocate them which is what I would like to avoid. (And I'm not actually sure that it would work anyway...)

Do you have any ideas?

Thank you very much,

Mo7Mae

0 Kudos
3 Replies
psantos
Beginner
2,891 Views
Hello mo7mae,

The problem is not related with the array declaration in the module. It is related with the way you declared the array. In fact you used var1 to declare the dimension of var3, being var1 an uninitialized integer. So the compiler doesn't know the dimension at the time of compilation.
I would suggest you to declare var3 to be allocatable and allocate it when you know the dimension. If you want to avoid the allocatable arrays and you know the value of var1 at the time of compilation you can do something like:

[fortran]module mod  
   
    integer, parameter :: var1 = 100 !value you want  
   
    real(8) :: var2  
   
    integer, dimension(var1) :: var3  
   
end module [/fortran]
Note that var1 is a constant, and because of this you can't change it during program execution. I only recommend this approach if you are 100% sure that var1 will have always the same dimension. If this is not the case, I think allocatable arrays is the way to go.
Hope this helps.

Pedro
0 Kudos
mecej4
Honored Contributor III
2,891 Views
The compiler can automatically allocate local arrays on the stack and relinquish that storage at subprogram exit, if the array size is either constant or is determined by integer arguments to the subprogram. This feature, however, will not help in the case of the module declaration, where array sizes must be known at compile time.

You also have another set of errors that you will eventually run into: a variable imported from a module cannot clash with a local variable or a subprogram argument.

Thus, all three arguments to BLA clash with variables declared in the module MOD which it imports. You will have to change the names to avoid the clash, or explicitly suppress importing the specific variable that is in conflict by adding a suitable renaming clause to your USE statements.
0 Kudos
John_Campbell
New Contributor II
2,891 Views
The use of variable var1,var2,var2 as both an argument of bla and bla2 and also declared in module mod can sometimes be allowed by compilers but should not be legal. It certainly is not clear coding.

John
0 Kudos
Reply