Fortran Problem with fortran 90 Present() function

  • Thread starter Thread starter sghan
  • Start date Start date
  • Tags Tags
    Fortran Function
AI Thread Summary
The discussion centers on the behavior of the Present function in Fortran when checking for optional variables in subroutines. The user encountered an issue where Present(c) returned TRUE even when the optional variable c was not passed to the subroutine test_present. The problem was resolved by placing the subroutine within a module and using that module in the main program. This change allowed Present(c) to function correctly, returning FALSE when c was not provided. The user seeks clarification on why this module structure is necessary for Present to work as expected.
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 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 BvU
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
25
Views
3K
Replies
4
Views
2K
Replies
59
Views
11K
Replies
4
Views
2K
Replies
8
Views
2K
Replies
4
Views
2K
Back
Top