Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28507 Discussions

PRESENT(X) where X is an optional dummy argument with INTENT(OUT)

OP1
New Contributor II
567 Views

I vaguely remember that a while back there was a discussion about the following question - but I forgot what the conclusion was...

Is it standard-conformant to query (with the PRESENT intrinsic) the presence of a dummy argument that has the intent OUT?

I am asking because it is (kind of) similar to the question of whether it is standard-conformant to query (with the ALLOCATED intrinsic) the allocation status of an allocatable dummy argument that has the intent OUT [and the answer here is: no this is not standard conformant].

0 Kudos
4 Replies
FortranFan
Honored Contributor II
547 Views

Per the standard document, `INTENT` attribute of the received argument does not affect the specific semantics of `PRESENT`.  The PRESENT intrinsic shall return true if the received argument corresponds to an actual present argument or to an argument with an allocated ALLOCATABLE attribute or an associated pointer object; false otherwise.

0 Kudos
Steve_Lionel
Honored Contributor III
521 Views

INTENT(OUT) does have a semantic effect in that the associated effective argument becomes undefined at entry to the procedure. If the argument is allocatable and allocated, it is deallocated.  However, this doesn't affect the use of PRESENT. For example:

program main
implicit none
integer, allocatable :: foo
allocate (foo)
call sub (foo)
contains

subroutine sub (bar)
integer, allocatable, intent(out), optional :: bar
print *, "bar is present: ", present(bar)
print *, "bar is allocated: ", allocated(bar)
end subroutine sub

end program main
D:\Projects>ifort t.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.10.0 Build 20230609_000000
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.36.32532.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

D:\Projects>t.exe
 bar is present:  T
 bar is allocated:  F
OP1
New Contributor II
513 Views

ok, thanks for the answers, I stand corrected on both points. I suspect that I picked up the practice of using only INTENT(IN) or INTENT(INOUT) for optional (and for allocatable) arguments a really long time ago when the implementation of those features were still new in the compilers and a bit buggy.

0 Kudos
Steve_Lionel
Honored Contributor III
469 Views

I'll comment that FortranFan's mention of unallocated allocatables or disassociated pointers being considered not present is a fairly recent addition to the language (F2008 or later). This applies only if the dummy argument is NOT allocatable/pointer.

0 Kudos
Reply