Problem with fortran 90 Present() function

  • Context: Fortran 
  • Thread starter Thread starter sghan
  • Start date Start date
  • Tags Tags
    Fortran Function
Click For Summary
SUMMARY

The Fortran 90 Present() function behaves unexpectedly when checking for optional variables in certain contexts. In the provided example, the subroutine test_present incorrectly returns TRUE for the optional variable 'c' when it is not passed in the call. This issue arises because the subroutine must be defined within a module, and the module must be used in the main program for Present() to function correctly. When the variables 'a' and 'b' are of type integer, Present() returns FALSE as expected, highlighting a type-specific behavior.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with optional arguments in Fortran subroutines
  • Knowledge of modules in Fortran programming
  • Experience with logical data types in Fortran
NEXT STEPS
  • Study the use of modules in Fortran to understand scope and visibility
  • Learn about the behavior of optional arguments in Fortran subroutines
  • Investigate the differences in handling logical values between different data types in Fortran
  • Explore best practices for defining and using subroutines in Fortran 90
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with optional arguments and subroutine definitions, as well as educators teaching Fortran programming concepts.

sghan
Messages
10
Reaction score
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
There are some examples and discussion here. Hope this helps.
 
  • Like
Likes   Reactions: BvU
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   Reactions: BvU

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K