Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: Passing integers to type dimension

  1. Sep 27, 2007 #1
    Why does does the following code not compile?

    Code (Text):

          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.
     
  2. jcsd
  3. Sep 27, 2007 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Sep 27, 2007 #3

    rcgldr

    User Avatar
    Homework Helper

    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: Sep 27, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Fortran: Passing integers to type dimension
Loading...