Print

Print


--On Monday, May 10, 2004 3:08 PM +0100 celevic <[log in to unmask]>
wrote:

> Hallo everybody,
> it seems that is not possible defining constants inside modules using
> intrinsic functions. Any trick?
> Example:
>
> MODULE constants
> IMPLICIT NONE
> REAL(8), PARAMETER  :: a=10
> REAL(8)             :: b
> b=sqrt(a)
> END MODULE constants
>
> Error: This statement must not appear in the specification part of a
> module b=sqrt(a)

That's because your b is *NOT* a constant.  It is a plain old ordinary
variable.  The b=sqrt(a) statement is an executable statement, not a
declaration.  Modules aren't executed, so this makes no sense.  (Procedures
in modules can be executed, but not the module itself).

Your main problem here has nothing to do with modules.  Since you
show that you do know how to declare a constant (your "a" is one),
I assume, though you didn't say, that you tried to declare b the same
way and it didn't work (as it wouldn't have).  That's because you
can't use the sqrt intrinsic in initialization expressions.  "Initialization
expression" is standard-speak for something that must be evaluated at
compile time.  Constants must be defined by initialization expressions.

The upcoming f2003 standard does allow sqrt in this role, but none of the
prior versions did.  Up through f95, you'll have to use an executable
statement to define sqrt - as your code above tries to do.  You'll need to
find something that is actually executed to put the statement in.  A module
procedure would do.  You could have a module procedure named, perhaps,
initialize_constants.  DOn't forget to call it from somewhere.

--
Richard Maine                |  Good judgment comes from experience;
[log in to unmask]       |  experience comes from bad judgment.
                             |        -- Mark Twain