Fortran Fortran: Passing integers to type dimension

AI Thread Summary
The code fails to compile due to a type mismatch between the main program and the subroutine. In Fortran, variable names starting with letters I-N are treated as INTEGER by default, while those starting with A-H and O-Z are treated as REAL. In the provided code, the main program defines "A" as an INTEGER array, but the subroutine does not explicitly declare the type of "A," leading to it being interpreted as REAL. This discrepancy causes the compilation error. It is noted that in Fortran, parameters are passed by address, so the subroutine must recognize "A" as an INTEGER. To resolve the issue, the subroutine can declare "A" as INTEGER with a size of 1, which allows it to compile correctly. Additionally, the discussion highlights that most compilers do not enforce strict checks on valid indexes, which can lead to further confusion.
dimensionless
Messages
460
Reaction score
1
Why does does the following code not compile?

Code:
      PROGRAM TYPES
      INTEGER A(3)      
      A(1)=1
      A(2)=2
      A(3)=3      
      CALL SUBR(A)
      print *,'Done'
      RETURN
      END            
    
C --- Here is a subroutine -----
      SUBROUTINE SUBR(A)
      DIMENSION A(3) 
      RETURN
      END

For some reason it compiles when I change the word "INTERGERS" to "REAL," but it doesn't compile as is.
 
Technology news on Phys.org
In the subroutine A is a REAL variable, since you didn't specify the type.

By default, variable names starting with letters I-N are INTEGER, A-H and O-Z are REAL.

Variables in each subprogram are independent of each other. The variable name A in the subroutine is independent of A in the main program.
 
Also in your comment, you spelled it "INTERGER" a mistake that a co-worker of mine in the 1970's constantly made, then wondered why the variables afterwards became "REAL".

In Fortran, parameters are passed by address, not by value, so the subroutine needs to know that "A" is an integer as well, but doesn't need to know the size, so you could just delcare "INTEGER A(1)" in the subroutine. Most compilers don't check for valid indexes anyway. Regarding passing parameters by address, this was always interesting depending on the compiler:

CALL SUBR(1.0)
A = 1.0
...

SUBROUTINE SUBR(X)
X = 2.0
RETURN
END
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
25
Views
3K
Replies
59
Views
11K
Replies
3
Views
2K
Replies
8
Views
4K
Replies
4
Views
2K
Replies
4
Views
2K
Back
Top