module - Can we avoid creating a local variable if an optional argument is not PRESENT? -
i having problem present statement fortran 95. using silverfrost's plato , ftn95 compiler (in "release win32" mode). wanted create subroutine sub(a,b), b optional variable. far good, problem arises when try give new value b if (.not. present(b)) b=0. code:
module mod contains subroutine sub(a,b) implicit none integer :: integer,optional :: b if (.not. present(b)) b=0 print*, a,b end subroutine sub end module mod program test use mod implicit none integer :: i=2, j=1 call sub(i,j) call sub(i) call sub(j) end program test is there elegant way out of situation, or need create variable, b_aux instance, , use following code?:
if (present(b)) b_aux=b else b_aux=0 endif
you can't use non-existent variable, approach such auxiliary local variable needed.