Fortran90: present() function doesn't work in subroutines

In summary: Therefore, in summary, code (a) works correctly because the subroutine is declared within the program block, while code (b) does not work correctly because the subroutine is not included in the program block.
  • #1
isotoper
1
0
I am using Gfortran 4.4.3 on Ubuntu 10.04 operating system.
Can someone explain why the following code works correctly (a):-

program testpresent
real :: a,b,c
call test_present(a,b,c)
contains
subroutine test_present(x1,x2,x3,x4,x5,x6)
real, intent(inout), optional :: x1,x2,x3,x4,x5,x6
write(6,*) present(x1),present(x2),present(x3),present(x4),present(x5),present(x6)
return
end subroutine test_present
end program

works correctly (T T T F F F) while (b):-

program testpresent
real :: a,b,c
call test_present(a,b,c)
end program

subroutine test_present(x1,x2,x3,x4,x5,x6)
real, intent(inout), optional :: x1,x2,x3,x4,x5,x6
write(6,*) present(x1),present(x2),present(x3),present(x4),present(x5),present(x6)
end subroutine test_present

does not. There is nothing in the documentation that says (b) is wrong, and it compiles without error messages.
 
Technology news on Phys.org
  • #2
The reason why the code in (b) does not work correctly is because the subroutine containing the call to the present function is not included in the program block. In Fortran, any subroutines containing calls to functions must be declared within the program block, otherwise the program will not be able to access the subroutine and thus will not be able to execute the desired code.
 

1. Why is my present() function not working in my Fortran90 subroutine?

The present() function is used to check if a variable or array has been allocated in memory. If it is not working in your subroutine, it could be because you have not properly declared the variable or array as allocatable in both your main program and subroutine.

2. Can the present() function be used to check the status of variables or arrays in a different subroutine?

Yes, the present() function can be used to check the status of variables or arrays in any subroutine as long as they have been declared as allocatable. However, the subroutine must have access to the same allocation status information as the main program.

3. Why does the present() function return an error code instead of a logical value?

The present() function returns an integer error code if the variable or array is not allocated, and a logical value of .TRUE. if it is allocated. This is a language feature of Fortran90 and is intended to make it easier to handle errors in memory allocation.

4. Is there a way to use the present() function to check the size of an array?

No, the present() function only checks if the array has been allocated or not. If you need to check the size of an array, you can use the size() function, which returns the number of elements in the array.

5. Can I use the present() function to check the status of pointers?

Yes, the present() function can also be used to check the status of pointers. If a pointer is not associated with a target, the present() function will return an error code. If it is associated with a target, it will return a logical value of .TRUE..

Similar threads

  • Precalculus Mathematics Homework Help
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
7K
  • High Energy, Nuclear, Particle Physics
Replies
3
Views
1K
Replies
5
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Back
Top