Prev: Clarify An Old Question: define a user data containing array with dimensions decided in running time
Next: parameter inconsistency (now adjustable dimensions of arguments)
From: ivan martin on 19 May 2010 13:26 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
From: mecej4 on 19 May 2010 13:44 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. Whether or not a parameter is used in declaring them, arrays in the main program declared as such are allocated for the duration of the entire program and have fixed sizes. Many algorithms implemented in subroutines, on the other hand, do pretty much the same thing regardless of the size of the arguments. I may want to compute the dot product of two 3-vectors today, 95-vectors tomorrow. This is where "variable dimensions" and looping constructs come in. Subroutines are written with assumed size arguments to accommodate this need. In your subroutine, the declaration of "array" as an array of size n does not allocate an array of that size; rather, the declaration gives information to the compiler to process the array properly, whatever be the size of the array passed to the subroutine. -- mecej4
From: ivan martin on 19 May 2010 13:59 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. -- Ivan
From: mecej4 on 19 May 2010 14:08 On 5/19/2010 12:59 PM, 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). I covered that in the second part of my answer. The reason is that N has no defined value at compile time, and it is our intent to be able to call the subroutine with any (reasonable) value of N, and an array Array(N) of that size; very often, in a single execution of the program the subroutine may be called hundreds of times, with different arrays and values of N. The adjective "dummy", used in Fortran to decorate what are called "formal arguments" in other languages, is quite meaningful. The topic is one that is covered in tutorials and textbooks. It is applicable to all high-level programming languages, e.g., C. > > 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. > Yes, because the value of N is not known at compile time and one cannot make a static allocation of unknown size. > -- Ivan -- mecej4
From: Gordon Sande on 19 May 2010 15:15
On 2010-05-19 14:59:29 -0300, ivan martin <i.martin(a)invalid.com> said: > 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). Because the subroutine might be called elsewhere with a different value. The compiler is not asked to read all your code to see if there is only one use. You might not write the other use until tomorrow! > 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). NO! When you use parameter it is like using a text editor to substitute the text. That is called a named constant. But the form immediately above is an initialization only. You can change N whenever you want to. You have just asked the loader to start N with a value of 100. Try making some integer a parameter and then assigning it a new value. You should get a diagnostic. If not then file a compiler bug report. > > 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. Because the loader hasn't had a chnace to set N yet. This is a really lousy wat to put it but it does the job. :-( > -- Ivan |