From: Dave Allured on
ivan martin wrote:
>
> On Wed, 19 May 2010 12:44:42 -0500, mecej4 <mecej4_no_spam(a)operamail.com> wrote:
>
> >On 5/19/2010 12:26 PM, ivan martin wrote:
> >> Maybe a naive question, but why do we need when in main program
> >> !************
> >> integer, parameter :: n=100
> >> real, dimension(n) :: my_array
> >>
> >> end
> >> !************
> >> but when in a subroutine
> >>
> >> !************
> >> subroutine my_subroutine(n,array)
> >> integer, intent(in) :: n
> >> real, dimension(n), intent(inout) :: array
> >> <snip some code>
> >>
> >> end subroutine my_subroutine
> >> !************
> >>
> >> e.g. where did the "parameter" go ?
> >> (I'm sure you understand what I'm aiming on by now)
> >>
> >> Shouldn't parameter keyword be used every time we wish to,
> >> for example, declare an array ?
> >>
> >> -- Ivan
> >
> >That is a valid, if naive, question.
> >
> >The use of a parameter (named constant) is not required, but using one
> >makes the code more readable and easier to modify. If I have 15 arrays
> >of size 100, and I want one day to enlarge them to size 200, with the
> >declared parameter I change one instance of 100 to 200, instead of 15
> >instances of 100.
>
> Hello mecej4,
> thanks for answering.
>
> You misunderstood though - I probably could've putted the question better also.
> I wasn't asking why about declaring size of arrays with parameters, instead of explcitly
> writing them out in declarations, but something else. I was wondering why when we do the above,
> in the main program need to state that something is a parameter
>
> INTEGER, PARAMETER : N
>
> but in the subroutine we just state
>
> INTEGER, INTENT(IN) :: N
>
> (e.g. we don't say that N is a parameter).
>
> Parameter == named constant ? Maybe ? But why does then work in the main program
> just to write
>
> INTEGER :: N=100
>
> (e.g. this is also a named constant).
>
> So my question is; when is the keyword PARAMETER used ? For example, if I try to declare the
> array with size N (the INTEGER without PARAMETER line above)
>
> REAL, DIMENSION(N) :: MY_ARRAY
>
> the compiler will give an error.

Perhaps you are looking for dynamic allocation. In the main program you
can do this:

INTEGER N
REAL, DIMENSION(N), ALLOCATABLE :: MY_ARRAY
...
read *, N
allocate (MY_ARRAY(N))

Look -- no parameter statement!

The subroutine does not need an allocate statement because ARRAY is only
a reference to another array in the calling program. In that case, the
memory space is already allocated. HTH.

--Dave
From: Craig Powers on
Dave Allured wrote:
>
> Perhaps you are looking for dynamic allocation. In the main program you
> can do this:
>
> INTEGER N
> REAL, DIMENSION(N), ALLOCATABLE :: MY_ARRAY

Should be DIMENSION(:), not DIMENSION(N), FYI.
From: glen herrmannsfeldt on
Craig Powers <craig.powers(a)invalid.invalid> wrote:
> Dave Allured wrote:

>> Perhaps you are looking for dynamic allocation. In the main program you
>> can do this:

>> INTEGER N
>> REAL, DIMENSION(N), ALLOCATABLE :: MY_ARRAY

> Should be DIMENSION(:), not DIMENSION(N), FYI.

As I wrote previously, in the PL/I form you can give the dimension
with the declaration, and override it on ALLOCATE, or not.
Bounds in the declaration are evaluated at the time of the ALLOCATE,
but from the scope of the declaration.

-- glen
From: robin on
"glen herrmannsfeldt" <gah(a)ugcs.caltech.edu> wrote in message news:ht2ea3$1lp$1(a)speranza.aioe.org...
| robin <robin51(a)dodo.com.au> wrote:
| > "mecej4" <mecej4_no_spam(a)operamail.com> wrote in message news:ht181t$8vf$1(a)news.eternal-september.org...
| > | On 5/19/2010 12:26 PM, ivan martin wrote:
| > | > Maybe a naive question, but why do we need when in main program
|
| > | > integer, parameter :: n=100
| > | > real, dimension(n) :: my_array
| (snip)
|
| > That used to be the case with FORTRAN 77 and earlier
| > (not using adjustable dimensions).
| > With Fortran 90 (which the OP is using), the use of PARAMETER
| > for array bounds is not warranted, for Fortran 90 +
| > have dynamic arrays. Thus, the bound (n) of an array could be
| > read in, and all arrays defined in terms of such a variable.
|
| I don't believe so. Note that the OP said MAIN. N would have
| to be defined before entry to MAIN, but no statements, especially
| no I/O, have yet been executed. It could be automatic or static,
| but must be a compile time constant.

No it doesn't. In this case, all the main progran has to do
is to read in N.
It then calls a subroutine (or function) to which it passes N.
That subroutine (or function) defines some or all of the various arrays
that are dependent on N.

| In PL/I you can do it with BEGIN, to start a new scope and
| new set of automatic variables.

And in Fortrnan, it's done as above, like I said.


From: robin on
"glen herrmannsfeldt" <gah(a)ugcs.caltech.edu> wrote in message news:ht44qb$801$1(a)speranza.aioe.org...
| Craig Powers <craig.powers(a)invalid.invalid> wrote:
| > Dave Allured wrote:
|
| >> Perhaps you are looking for dynamic allocation. In the main program you
| >> can do this:
|
| >> INTEGER N
| >> REAL, DIMENSION(N), ALLOCATABLE :: MY_ARRAY
|
| > Should be DIMENSION(:), not DIMENSION(N), FYI.
|
| As I wrote previously, in the PL/I form you can give the dimension
| with the declaration, and override it on ALLOCATE, or not.

That's not so.
Constant bounds given in the declaration cannot be overridden
by ALLOCATE.