From: Allamarein on
I'm getting experience with modules.
Therefore I try to compile a trivial statistical module for
educational purpose.
SUM function should sum the elements of an input array.
I would to not specify array size, but my function should detect that.
I tried the following code:

MODULE STATISTICS

CONTAINS

REAL FUNCTION SUM(X)
IMPLICIT NONE
INTEGER :: N
REAL :: RES=0.0
REAL, INTENT(IN), DIMENSION(:), ALLOCATABLE :: X

N = SIZE(X)
ALLOCATE(X(1:N))

DO I = 1,N
RES = RES+X(I)
END DO

DEALLOCATE(X)
SUM = RES
END FUNCTION SUM

END MODULE STATISTICS

Anyway I get some errors. I suppose my problems are in the allocatable
variables.
If a write a function where I declare array size X (e.g. SUM(X,N)
with N the array size), my module works properly. But I don't want
this...
What should I change my previous code?
Thank you in advance
From: dpb on
Allamarein wrote:
> I'm getting experience with modules.
> Therefore I try to compile a trivial statistical module for
> educational purpose.
> SUM function should sum the elements of an input array.
> I would to not specify array size, but my function should detect that.
> I tried the following code:
>
> MODULE STATISTICS
>
> CONTAINS
>
> REAL FUNCTION SUM(X)
> IMPLICIT NONE
> INTEGER :: N
> REAL :: RES=0.0
REAL, INTENT(IN) :: X(:)
>
> N = SIZE(X)
> DO I = 1,N
> RES = RES+X(I)
> END DO
>
> SUM = RES
> END FUNCTION SUM
>
> END MODULE STATISTICS
>
> Anyway I get some errors. I suppose my problems are in the allocatable
> variables.

Indeed.

You don't want an allocatable array here, you want an assumed-shape
array where the number of dimensions is specified by the number of
colons in the dimension expression and the size is that of the passed
argument. It requires an explicit interface (which you get w/ the
module automagically)...

--
From: Allamarein on
On 26 Giu, 14:53, dpb <n...(a)non.net> wrote:
> Allamarein wrote:
> > I'm getting experience with modules.
> > Therefore I try to compile a trivial statistical module for
> > educational purpose.
> > SUM function should sum the elements of an input array.
> > I would to not specify array size, but my function should detect that.
> > I tried the following code:
>
> > MODULE  STATISTICS
>
> >            CONTAINS
>
> >            REAL FUNCTION  SUM(X)
> >                    IMPLICIT  NONE
> >                    INTEGER ::  N
> >                    REAL :: RES=0.0
>
>    REAL, INTENT(IN) :: X(:)
>
>
>
> >                       N = SIZE(X)
> >                    DO I = 1,N
> >                            RES = RES+X(I)
> >                    END DO
>
> >                    SUM = RES
> >            END FUNCTION  SUM
>
> > END MODULE  STATISTICS
>
> > Anyway I get some errors. I suppose my problems are in the allocatable
> > variables.
>
> Indeed.
>
> You don't want an allocatable array here, you want an assumed-shape
> array where the number of dimensions is specified by the number of
> colons in the dimension expression and the size is that of the passed
> argument.  It requires an explicit interface (which you get w/ the
> module automagically)...
>
> --

I found something about 'assumed-shape' array, but I don't understand
very well.
Would you suggest how you'd modify my code?
I don't know how I can use an 'explicit interface'.
Thank for your support

From: dpb on
Allamarein wrote:
....

> I found something about 'assumed-shape' array, but I don't understand
> very well.

I'd suggest reading the language reference manual w/ the compiler or
programmers' guide or buy/check out one of the Fortran reference books
available. You'll need it anyway for more than just this...

> Would you suggest how you'd modify my code?

I did in previous post. Look at the lines I moved to left and are not
quoted any longer as well as having removed references to allocatable
arrays.

> I don't know how I can use an 'explicit interface'.

As I also said in the previous post, the use of the module automagically
generates the explicit interface for you...if you don't follow that, see
above about resources...

--
From: dpb on
dpb wrote:
> Allamarein wrote:
> ...
>
>> I found something about 'assumed-shape' array, but I don't understand
>> very well.
>
> I'd suggest reading the language reference manual w/ the compiler or
> programmers' guide or buy/check out one of the Fortran reference books
> available. You'll need it anyway for more than just this...
....

BTW, I see from another posting you're using the IVF compiler -- by
comparison to the product of the current Intel team that I have, I'm
quite sure the documentation that came w/ the compiler is quite thorough
in having both document types outlined above -- the Guide that is more
general on Fortran language features as well as the specific language
syntax manual. Use them.

As it seems you're new to Fortran, I can't emphasize enough the
usefulness of a a good reference book (or two). There are a multitude
of threads in clf, some within the last couple of weeks even on
recommendations/discussion of strengths of various titles currently
available.

--