Problem with fortran 90 Present() function

In summary, the conversation discusses a problem with the Present(x) function returning TRUE even when the optional variable is not present in the subroutine call. It is found that this issue can be resolved by placing the subroutine in a module and using it in the main program. The reason for this is unclear and further explanation is requested.
  • #1
sghan
10
1
Hi all,
When I use Present(x) to check for the presence of an optional variable, I am getting TRUE even when the subroutine call clearly does NOT contain the optional variable. See here the call to test_present(a,b):

program options
implicit none

real*8 a,b
real*8 c
a = 10.d0
b = 5.d0
c = 3.d0
call test_present(a,b)

end program options

! In a separate file:
subroutine test_present(a,b,c)
implicit none

real*8 a,b
real*8, optional :: c
logical value

value = present(c)
write(*,*)"is c present?", value !THIS RETURNS TRUE!

end subroutine test_present


Why does present(c) return TRUE even though c is not given in the call? Strangely, if variables a,b are of type integer, present(c) returns false as it should.
Thanks!
 
Technology news on Phys.org
  • #2
There are some examples and discussion here. Hope this helps.
 
  • Like
Likes BvU
  • #3
Thanks. Found the answer: in the program above, I'd have to put the subroutine in a module, then "use module" in the program. Only then does Present(x) work properly. If anyone wants to explain why this is necessary, I would appreciate it!
 
  • Like
Likes BvU

Related to Problem with fortran 90 Present() function

What is the purpose of the "Present()" function in Fortran 90?

The "Present()" function in Fortran 90 is used to check if a variable has been allocated a value. It returns a logical value of "true" if the variable has been assigned a value and "false" if it has not.

How do you use the "Present()" function in a Fortran 90 program?

To use the "Present()" function, you need to declare it in the program's header by including the "USE ISO_FORTRAN_ENV" statement. Then, you can use "Present()" in an IF statement to check the status of a variable.

What are the potential problems with using the "Present()" function in Fortran 90?

One potential issue with using the "Present()" function is that it only checks if a variable has been allocated a value, but it does not check the value itself. This means that a variable could have a value of zero or null, but "Present()" would still return "true".

Another problem with "Present()" is that it only works for scalar variables and not arrays or derived types. In these cases, you would need to use other methods to check the status of the variable.

How can you troubleshoot errors with the "Present()" function in Fortran 90?

One way to troubleshoot errors with the "Present()" function is to use the DEBUG compiler option, which will give you more detailed information about the error. You can also use PRINT statements to check the value of the variable before and after the "Present()" function is called.

Can the "Present()" function be used with other Fortran versions?

The "Present()" function was first introduced in Fortran 90 and is also available in Fortran 95 and later versions. However, it is not compatible with earlier versions of Fortran. If you are using an older version, you will need to use alternative methods to check the status of a variable.

Similar threads

  • Programming and Computer Science
Replies
25
Views
861
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
963
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
Back
Top