Fortran: Passing integers to type dimension

In summary, the given code does not compile because the variable A is not properly declared as an integer in the subroutine. Changing the word "INTEGER" to "REAL" allows the code to compile, but this may not be the desired result. Additionally, parameters are passed by address in Fortran, so the subroutine needs to know the type of variable being passed. This can be achieved by declaring "INTEGER A(1)" in the subroutine.
  • #1
dimensionless
462
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
  • #2
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.
 
  • #3
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:

1. What is Fortran and how is it used in scientific computing?

Fortran (Formula Translation) is a high-level programming language commonly used in scientific computing for numerical and scientific applications. It is known for its efficiency in handling complex mathematical calculations and its ability to work with large arrays of data.

2. What is meant by "passing integers to type dimension" in Fortran?

In Fortran, arrays are declared with a specific size or dimension. When passing an integer to the type dimension, it means specifying the size of the array using an integer value. This allows the program to dynamically allocate memory for the array at runtime.

3. How do you pass an integer to type dimension in Fortran?

To pass an integer to type dimension in Fortran, you need to declare the array with a specific size and use the integer value as the dimension parameter. For example, "integer :: array(10)" would declare an array with 10 elements.

4. Can you pass a variable as the dimension in Fortran?

Yes, you can pass a variable as the dimension in Fortran. This allows for more flexibility as the size of the array can be determined at runtime. However, the variable used as the dimension must be declared as an integer.

5. What is the benefit of passing integers to type dimension in Fortran?

Passing integers to type dimension in Fortran allows for dynamic memory allocation, making it easier to work with large arrays of data. It also allows for more flexibility in the size of the array, as it can be determined at runtime based on the needs of the program.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
608
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top