Hello,
Yet another problem: I have a 3rd party code in which there is a "chain" of optional argument passing done without checking with 'present'.
It looks like tnis (example, I/O is done to use unpredictable data, to avoid compiler optimizations):
module m0016
contains
subroutine f(j)
implicit none
integer,intent(in),optional::j
integer i
write(*,*)'f(): enter i:'
read(*,*)i
call g(i,j)
return
end subroutine f
subroutine g(i,j)
implicit none
integer,intent(in)::i
integer,intent(in),optional::j
write(*,*)'g(): i=',i
if(present(j)) write(*,*)'g(): j=',j
return
end subroutine g
end module m0016
program test
use m0016
implicit none
integer::j
call f()
write(*,*)'main: enter j:'
read(*,*)j
call f(j)
stop
end program test
This code compiles and surprisingly works...
However use of j in f() in call to g() without 'present' looks strange...
Is such a careless use of optional arguments correct and allowed by Fortran standards?
連結已複製
3 回應
Yes, this is allowed. The present-status of optional arguments is carried through. I am not sure why you call this "careless", it can be a very useful property. It does require that an explicit interface be visible at all steps.
Compiler doc (on kwrd OPTIONAL) doesn't clarify this question and examples attached also do not cover the matter (<- but please don't read this as a complaint).
That's why I decided to ask this Q.
And yes, I admit: this property is useful - it decreases amount of code, delegating the task to compiler or some additional runtime code (?) generated implicitly.
See the additional topic "Optional Arguments" that is referenced at the bottom of the OPTIONAL page. The description of OPTIONAL is about the syntax term - the concept of omitted arguments is more complex and discussed elsewhere. I'll also comment that the compiler reference is not intended as a tutorial on the language.
