Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

The number of subscripts is incorrect

harsha_vss
Novice
451 Views

I am attaching the basic code. I'm a beginner in Fortran. Can anybody help me with this error?

 

program check
implicit none
real(8) :: y(2,2)
integer :: i,j
real(8), dimension(2,2) :: u, deep
real(8) :: x = 100 , z = 50
do i = 1,2
do j = 1,2
y(i,j) = i+j
end do
end do
u = deep(x,y,z)
end program check


function deep (x,y,z) result (t)
implicit none
real(8) :: x , z , h
real(8), dimension (2,2) :: y, t
!real(8), dimension (2,2) :: deep
integer :: i,j

h = x+z
t = h*y

end function

0 Kudos
1 Solution
Arjen_Markus
Valued Contributor III
435 Views

Well, it is quite simple: the program contains a declaration of an array deep which has dimension 2. Later in the program you use it with three indices - you probably meant to use the function deep that appears after the program itself, but the compiler first sees the declaration and the incorrect use of the array.

Here is my correction of the program:

module deepfunc
    implicit none
contains
    function deep (x,y,z) result (t)
    implicit none
    real(8) :: x , z , h
    real(8), dimension (2,2) :: y, t
    !real(8), dimension (2,2) :: deep
    integer :: i,j

    h = x+z
    t = h*y

    end function
end module deepfunc

program check
    use deepfunc
    implicit none
    real(8) :: y(2,2)
    integer :: i,j
    real(8), dimension(2,2) :: u
    real(8) :: x = 100 , z = 50
    do i = 1,2
    do j = 1,2
    y(i,j) = i+j
    end do
    end do
    u = deep(x,y,z)
end program check

By putting the function in a module you achieve that the compiler knows that is a function, what results it gives and what the arguments are.

You could achieve that in a number of ways, but using modules is a robust way to make sure the compiler can check things.

View solution in original post

5 Replies
Arjen_Markus
Valued Contributor III
436 Views

Well, it is quite simple: the program contains a declaration of an array deep which has dimension 2. Later in the program you use it with three indices - you probably meant to use the function deep that appears after the program itself, but the compiler first sees the declaration and the incorrect use of the array.

Here is my correction of the program:

module deepfunc
    implicit none
contains
    function deep (x,y,z) result (t)
    implicit none
    real(8) :: x , z , h
    real(8), dimension (2,2) :: y, t
    !real(8), dimension (2,2) :: deep
    integer :: i,j

    h = x+z
    t = h*y

    end function
end module deepfunc

program check
    use deepfunc
    implicit none
    real(8) :: y(2,2)
    integer :: i,j
    real(8), dimension(2,2) :: u
    real(8) :: x = 100 , z = 50
    do i = 1,2
    do j = 1,2
    y(i,j) = i+j
    end do
    end do
    u = deep(x,y,z)
end program check

By putting the function in a module you achieve that the compiler knows that is a function, what results it gives and what the arguments are.

You could achieve that in a number of ways, but using modules is a robust way to make sure the compiler can check things.

harsha_vss
Novice
429 Views
mecej4
Black Belt
409 Views

When a function is used that does not return a scalar value, among other situations, the rules of the language require that an interface be available to the compiler. Arjen showed you one way of providing that interface. Another way is to make the function a contained function:

program check
implicit none
real(8) :: y(2,2)
integer :: i,j
real(8), dimension(2,2) :: u
real(8) :: x = 100 , z = 50
do i = 1,2
do j = 1,2
y(i,j) = i+j
end do
end do
u = deep(x,y,z)

contains

function deep (x,y,z) result (t)
implicit none
real(8) :: x , z , h
real(8), dimension (2,2) :: y, t
!real(8), dimension (2,2) :: deep
integer :: i,j

h = x+z
t = h*y

end function
end program check
harsha_vss
Novice
383 Views

Thanks, Mecej4! I understood that concept now.

jimdempseyatthecove
Black Belt
371 Views

Implicit in Markus and mecej4's postings is that you develop an understanding as to if your function deep is intended to be used only by a single calling procedure .OR. by multiple calling procedures.

In the case of single calling procedure, then the contained procedure method may be preferred.

In the case of multiple calling procedures .OR. potential re-use by you in some future project, then use the module contained procedure (and make the module a separate source file).

We (this forum) have many experienced programmers willing to help beginners learn good programming techniques.

Jim Dempsey

Reply