Can equations be used in Fortran array declarations with parameters?

  • Context: Fortran 
  • Thread starter Thread starter Matterwave
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary

Discussion Overview

The discussion revolves around the use of equations in Fortran array declarations, specifically whether parameters can be utilized in defining array dimensions. Participants explore the rules surrounding array bounds and the distinction between parameters and variables in Fortran.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant questions if equations can be used in array declarations, providing an example with a variable.
  • Another participant asserts that array bounds must be INTEGER constants, but constants declared with the PARAMETER statement are permissible if declared before the array.
  • A participant speculates that even if the variable were a parameter, it might still not be allowed, but later references a lecture suggesting that it could be acceptable if y is indeed a parameter.
  • Discussion includes clarification on the nature of parameters in Fortran, emphasizing that their values are known at compile time, contrasting with variables that are assigned values at runtime.
  • A participant corrects a previous statement by clarifying the difference between assignment and equality testing in programming languages, specifically in the context of Fortran's syntax.
  • Another participant acknowledges the confusion regarding terminology and seeks confirmation that their statement about array dimensions is valid if y is a parameter.

Areas of Agreement / Disagreement

Participants express differing views on the use of equations in array declarations, with some asserting that only INTEGER constants are allowed while others suggest that parameters may be used. The discussion remains unresolved regarding the specific conditions under which parameters can be utilized in this context.

Contextual Notes

Limitations include the need for clarity on the definitions of parameters versus variables and the specific rules governing array declarations in Fortran, which are not fully resolved in the discussion.

Matterwave
Science Advisor
Homework Helper
Gold Member
Messages
3,971
Reaction score
329
Hi, I'm just wondering real quick, are equations allowed in array declarations? For example:

int :: y=2
int, dimension(2*y):: x

?
 
Technology news on Phys.org
I don't think the equation you have would be permitted, as the array bounds must be INTEGER constants. However, constants declared with the PARAMETER statement are OK for array declarations, as long as the PARAMETER statement precedes the array declaration.

http://star-www.st-and.ac.uk/~spd3/Teaching/AS3013/lectures/AS3013_lecture5.pdf
 
So even if y were a parameter, probably I still can't do that?

EDIT: I looked at a later part of the lecture and he had something like what I have except y is a parameter. So I think I'm doing OK. Thanks. :)
 
The whole point of a parameter in Fortran is that the parameter's value is known at compile time, so the compiler knows how much memory to allocate for the array. For a variable (not a parameter), the variable gets its value at run time, I believe.

Minor point. A statement such as y = 2 is an assignment, or in this case an initialization, not an equation. It does not state that y and 2 are equal. Instead, it stores the constant value 2 in the location that is named y.

Most programming languages make distinctions between assignment and testing for equality by using different symbols. For example, Fortran uses .EQ. to test for equality, and uses = for assignment. Languages based on C, such as C++, C#, Java, and others, use == to test for equality and = for assignment. Newer versions of Fortran now support the C-style notation, in addition to the older .EQ., .LT., GT., and other spelled-out relational operators.
 
Yes, I forgot to include that y is a parameter, and my language is lazy because I'm not a programmer really. Sorry for the confusion. :P

I just wanted to make sure that the statement

real, dimension(2*y)::x

is legitimate if y is a parameter, and Steamking's link had something just like that in it.
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
Replies
64
Views
11K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K