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

Q about careless passing of optional arguments to furhter calls

Witold_I_
Beginner
234 Views
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?
0 Kudos
3 Replies
Steven_L_Intel1
Employee
234 Views

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.

0 Kudos
Witold_I_
Beginner
234 Views
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.
0 Kudos
Steven_L_Intel1
Employee
234 Views

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.

0 Kudos
Reply