- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page